简体   繁体   English

如何在iOS 8/9中获取今天扩展的宽度

[英]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. 我目前正在尝试创建一个与旧版本的Today扩展以及iOS 10中的新样式小部件兼容的Today扩展。

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. 在iOS 10中很好用-widgetMaximumSizeForDisplayMode可用于在任何设备上获取小部件的宽度和高度。

However i'm struggling to figure out how to do this for iOS 8 / 9 as widgetMaximumSizeForDisplayMode is only available from iOS 10 onwards. 但是我正在努力弄清楚如何为iOS 8/9执行此操作,因为widgetMaximumSizeForDisplayMode仅从iOS 10开始可用。 Sometimes the today extension width is simply the width of the device as below: 有时,今天的扩展宽度就是设备的宽度,如下所示:

iPhone 6 today view iPhone 6今天视图

But for other devices like the ipad the width of the widget is only a certain width on the screen. 但是对于其他设备(例如ipad),小部件的宽度仅是屏幕上的特定宽度。

iPad Air today view iPad Air今日视图

Is there a built in method to grab the today-extension width in iOS 8 / 9 ? 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? 您是否尝试过在今天扩展的视图控制器中使用override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)方法?

It is supported from iOS 8 and above and you will get the new size on rotations too. iOS 8及更高版本支持该功能,并且您还将获得新的旋转尺寸。

https://developer.apple.com/reference/uikit/uicontentcontainer/1621466-viewwilltransitiontosize 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 . 但是,如果您需要阅读它以确定该宽度的高度,则只需检查view.bounds.width

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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