简体   繁体   中英

How to get the width of a today-extension in iOS 8 / 9

I'm currently trying to create a today extension that is compatible with the old-style today extension as well as the new style widget in iOS 10.

I am not using storyboards, and so I need to create and set the height and width of the widget's view in the view controller class. This is fine in iOS 10 - widgetMaximumSizeForDisplayMode can be used to grab the width and height of the widget on any device.

However i'm struggling to figure out how to do this for iOS 8 / 9 as widgetMaximumSizeForDisplayMode is only available from iOS 10 onwards. Sometimes the today extension width is simply the width of the device as below:

iPhone 6 today view

But for other devices like the ipad the width of the widget is only a certain width on the screen.

iPad Air today view

Is there a built in method to grab the today-extension width in iOS 8 / 9 ?

Have you tried with the override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) method in the view controller for the today extension?

It is supported from iOS 8 and above and you will get the new size on rotations too.

https://developer.apple.com/reference/uikit/uicontentcontainer/1621466-viewwilltransitiontosize

The width is always the same for a given device/orientation--you can't set it. But if you need to read it in order to determine the height for that width, you can just check view.bounds.width .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM