Today Widget Extension Height - iOS10

…衆ロ難τιáo~ 提交于 2019-12-03 10:49:05

The height of the widget in iOS 10 is exactly 110 in compact mode. It can be set to whatever height you want in expanded mode, but in compact mode it will always be 110 and that can't be overwritten.

Try to set self.preferredContentSize at viewDidLoad method. Here is full code :

Swift 3

override func viewDidLoad() {

    super.viewDidLoad()

    self.preferredContentSize = CGSize(width:self.view.frame.size.width, height:210)

    if #available(iOSApplicationExtension 10.0, *) {
        self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
    }
}


 @available(iOS 10.0, *)
    @available(iOSApplicationExtension 10.0, *)
    func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
        if activeDisplayMode == .expanded {
            self.preferredContentSize = CGSize(width: self.view.frame.size.width, height: CGFloat(3)*self.tableView.rowHeight)
        }else if activeDisplayMode == .compact{
            self.preferredContentSize = CGSize(width: maxSize.width, height: 110)
        }
    }

please use your height for your application.

Hope will fix your issue.

To get the max height of the widgets active display mode, do this in your UIViewController

let context = self.extensionContext
if let context = context{
    let height = context.widgetMaximumSize(for: context.widgetActiveDisplayMode).height
}

To get the max height of expanded and compact individually regardless of the current display mode, do this:

var context = self.extensionContext
if let context = context{
    let compactHeight = context.widgetMaximumSize(for: .compact).height
    let expandedHeight = context.widgetMaximumSize(for: .expanded).height
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!