Today Widget Extension Height - iOS10
Asked Answered
P

3

21

The height for the Today's widget view mode cannot be set for compact Mode. No matter whatever value I set. It sets the height of the widget to a default value. The expanded mode works perfect and the value is properly set and reflected in the widget. I have already added this line in my viewDidLoad() method.

self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded

Here is the code. The value of maxSize cannot be changed too as its a constant.

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {

        //self.preferredContentSize = (activeDisplayMode == .compact) ? maxSize : CGSize(width: maxSize.width, height: 300)

        if activeDisplayMode == NCWidgetDisplayMode.compact
        {
             self.preferredContentSize = CGSize(width: maxSize.width, height: 300)
        }
        else
        {
            self.preferredContentSize = CGSize(width: maxSize.width, height: 560)
        }
}
Papist answered 17/11, 2016 at 11:5 Comment(3)
in compact mode 110 is the minimum height for widgets.Ruralize
@ConstantinSaulenco Thanks - but how can I change this value.Papist
in compact mode there is no way to change that valueRuralize
R
25

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.

Raymonderaymonds answered 22/11, 2016 at 16:24 Comment(3)
The height of the widget in iOS 10+ is variable based on the system text size. 110 is the height with the default/medium text size, but it can be less than or greater than 110 if you decrease or increase the system text size in the Settings app.Ahrens
This answer does not cover all the states actually. It should be merged with @GregG comment :)Beare
I wouldn't use this method going forward, please see my answerWaechter
C
17

Solution is setting the preferredContentSize at viewDidLoad method.

Here is the example:

Swift 3 and later

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)
        }
    }

Warning

You should use your specific height for your case. 110 is valid in my scenario.

Hope this will fix your issue.

Best

Crack answered 17/11, 2016 at 11:16 Comment(0)
W
2

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
}
Waechter answered 24/4, 2019 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.