Today Extension compact mode height in iOS 10
Asked Answered
G

4

17

I am struggling to change the height of my iOS 10 widget in compact mode.

All I have is an empty widget, no views inside it. Still, no matter what I set for the compact height, it seems to ignore it.

Here is my code:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self.extensionContext setWidgetLargestAvailableDisplayMode:NCWidgetDisplayModeExpanded];

}

- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize{
    if (activeDisplayMode == NCWidgetDisplayModeCompact) {
        self.preferredContentSize = CGSizeMake(0, 50);
    }
    else{
        self.preferredContentSize = CGSizeMake(0, 200);
    }
}

Could this be an issue with beta software? I am on Xcode 8 beta and iOS 10 beta 7.

Gosnell answered 29/8, 2016 at 9:47 Comment(5)
The height in compact mode can't be anything other than 110 from what I can tell.Graniela
But how come other apps work on iOS 10, with sizes less than 110?Busyness
All of apple's apps have 110 height in compact mode from what I can tell. Any third party you apps you have would have been built for iOS 9 so their extensions were built before the different display modes existed and will continue working the same that they did in iOS 9.Graniela
That could be the case, yes! Anyways, I filed a bug to Apple, if anything at least they will say the sameBusyness
in iOS 11 this height is 120 instead of 110Robinrobina
C
21

According to the What's new in Cocoa Touch session from WWDC 2016 (around the 44:00 mark):

Now we have a user controlled size. The compact mode, which is fixed height and the expanded mode which is variable height.

So, it seems that setting a preferredContentSize for NCWidgetDisplayModeCompact is completely ignored (the fixed size appears to be 110pts).

Cachet answered 9/9, 2016 at 15:51 Comment(2)
It is not completely ignored. I observe that you still have to set prefferedContentSize in widgetActiveDisplayModeDidChange when activeDisplayMode becomes compact. Otherwise it breaks 'show more/less' functionality.Labarum
Fixed size is NOT always 110pt, it is 130pt for iPad 12.9 (iOS12.1). Use extensionContext?.widgetMaximumSize(for: .compact).heightFleuron
P
8

In SWIFT 3:

The following property for your TodayViewController will return max size for compact mode:

private var maxSizeForCompactMode: CGFloat {
    return extensionContext?.widgetMaximumSize(for: .compact).height ?? 0
}
Patentor answered 10/5, 2017 at 11:47 Comment(1)
Can extensionContext be nil or can widgetMaximumSize return .zero size?Dedicate
P
7

1) Set the display mode to NCWidgetDisplayModeExpanded in viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
    self.extensionContext?.widgetLargestAvailableDisplayMode = NCWidgetDisplayMode.expanded
}

2) Implement this protocol method

func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize){
    if (activeDisplayMode == NCWidgetDisplayMode.compact) {
        self.preferredContentSize = maxSize;
    }
    else {
        self.preferredContentSize = CGSize(width: 0, height: 200);
    }
}

iOS 10 Widget

Pages answered 29/8, 2016 at 10:26 Comment(8)
Basically, you copy pasted my question and translated it to swiftBusyness
@BalázsVincze It's working for me with the above given code. Attached the screenshot for reference.Pages
Yes, the expanded size works for me too. I have problems with compact sizeBusyness
Ok. AFAIK that one is the limitation for iOS10. 110 is the minimum height for widgets.Pages
But the app Clear has sizes of 50 even iOS 10, plus changing the compact size to bigger than 110 does not work eitherBusyness
Is there a method we can listen to to know when the widget has finished animating its resize?Milda
@BalázsVincze the app Clear has not been updated since before iOS 10, so the widgets you're seeing were build for iOS 9 and so do not have the iOS 10 height rules applied to them. The easy way to tell is that old widgets have a different/darker coloured background.Everick
Liking the ";" usage even in swift XDFlowery
F
0

Set preferredContentSize in viewDidAppear or after then. I guess that widget will resize before view appears. The widget's width is NOT SCREEN_WIDTH because of gap between iPhone screen edge.

Francophile answered 7/3, 2017 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.