Balázs Vincze
Balázs Vincze

Reputation: 3832

Today Extension compact mode height in iOS 10

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.

Upvotes: 17

Views: 7895

Answers (4)

iamhite
iamhite

Reputation: 429

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.

Upvotes: 0

spassas
spassas

Reputation: 4818

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

Upvotes: 21

Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61774

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
}

Upvotes: 8

Pranay
Pranay

Reputation: 372

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

Upvotes: 7

Related Questions