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.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Balázs Vincze
  • 3,550
  • 5
  • 29
  • 60
  • The height in compact mode can't be anything other than 110 from what I can tell. – dan Aug 29 '16 at 16:44
  • But how come other apps work on iOS 10, with sizes less than 110? – Balázs Vincze Aug 29 '16 at 16:50
  • 5
    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. – dan Aug 29 '16 at 17:02
  • That could be the case, yes! Anyways, I filed a bug to Apple, if anything at least they will say the same – Balázs Vincze Aug 29 '16 at 17:04
  • in iOS 11 this height is 120 instead of 110 – BangOperator Jan 04 '18 at 11:27

4 Answers4

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

David
  • 3,285
  • 1
  • 37
  • 54
spassas
  • 4,778
  • 2
  • 31
  • 39
  • 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. – jesse Jan 25 '17 at 13:25
  • Fixed size is NOT always 110pt, it is 130pt for iPad 12.9 (iOS12.1). Use `extensionContext?.widgetMaximumSize(for: .compact).height` – Laevand Apr 24 '19 at 08:14
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
}
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
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

Koraktor
  • 41,357
  • 10
  • 69
  • 99
Pranay
  • 372
  • 1
  • 10
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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
iamhite
  • 439
  • 4
  • 7