Today Extension compact mode height in iOS 10

此生再无相见时 提交于 2019-12-20 17:41:01

问题


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.


回答1:


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




回答2:


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




回答3:


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
}



回答4:


set prefferedContentSize 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.



来源:https://stackoverflow.com/questions/39203189/today-extension-compact-mode-height-in-ios-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!