Get width of iOS today extension

2020-07-14 05:10发布

问题:

I want to get the width value of my widget / today extension.

I can't use self.view.frame.size.width because it returns the whole screen width and not the actual widget width (on the iPad the width of the extension is not equal to the screen width).

What can I do to get the actual width of the widget?

回答1:

.............. Don't read self.view.frame in viewDidLoad.

frame = (0 0; 768 1024); // in viewDidLoad

You should read self.view.frame after viewDidAppear

after viewDidAppear, self.view.frame is right value for content.

frame = (0 0; 545 75.5); // in viewDidAppear



回答2:

Instead of viewDidAppear, I found that viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator is triggered automatically when the widget gets resized initially