What is the height of the new iOS 10 Today Widget/Extension?

后端 未结 3 2013
孤街浪徒
孤街浪徒 2020-12-24 13:58

I am building an iOS Today widget, and while testing for iOS 10 I noticed that all widgets are now being given the same height (previous versions allowed the dev to set the

3条回答
  •  盖世英雄少女心
    2020-12-24 14:57

    The widget in iOS 10 has been changed as you have noticed and does now have a fixed height. There has also been added new features to the today extension. On of them is the NCWidgetDisplayMode. Basically you have a button up in the right corner where you can "Show more" or "Show less".

    Begin by adding the following to your viewDidLoad()

    self.preferredContentSize = CGSize(width: 0, height: 200)
    
    if #available(iOSApplicationExtension 10.0, *) {
        self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
    } else {
        // Fallback on earlier versions
    }
    

    What you then need to do is to basically add the following method:

    Swift version:

    @available(iOSApplicationExtension 10.0, *)
    func widgetActiveDisplayModeDidChange(activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
        if activeDisplayMode == NCWidgetDisplayMode.Compact {
            self.preferredContentSize = CGSizeMake(0.0, 200.0)
        }
        else if activeDisplayMode == NCWidgetDisplayMode.Expanded {
            self.preferredContentSize = desiredSize
        }
    
    }
    

    Objective-C version:

    - (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize{
        if (activeDisplayMode == NCWidgetDisplayModeCompact){
            self.preferredContentSize = CGSizeMake(0.0, 200.0);
        }
        else if (activeDisplayMode == NCWidgetDisplayModeExpanded){
            self.preferredContentSize = desiredSize;
        }
    }
    

    Note two things here:

    Xcode will automatically suggest that you add the available check for the iOS version (for Swift at least). So do not remove the old way to do this self.preferredContentSize = CGSizeMake... This is still needed for older iOS versions.

    In the widgetActiveDisplayModeDidChangefunction activeDisplayMode == NCWidgetDisplayMode.Compactwill be called when you go from "Show more" > "Show less". This is because it´s trigged immediately from the iOS system. And activeDisplayMode == NCWidgetDisplayMode.Expanded will be called when you go from "Show less" > "Show more".

    And one last thing, this is kind of buggy still with the "Show more" and "Show less" buttons and it´s not fixed yet by Apple. Check the demonstration from Apples Keynote and you will notice that he had the bug issue with this.

提交回复
热议问题