3

How do I change the height of an iOS 10 extension in compact mode? Or more broadly, how do I change the height of an extension without using widgetActiveDisplayModeDidChange?

Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
Noha Mohamed
  • 147
  • 1
  • 8

1 Answers1

2

Solution

Here is the solution to your problem:

override func viewDidLoad() {
    super.viewDidLoad()
        
    self.preferredContentSize = CGSize(width:self.view.frame.size.width, height:210)
        
    if #available(iOSApplicationExtension 10.0, *) {
        self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
    }
}

the height as 210 how I prefer, you can use any height of course...

Update

If you want to use a fixed size you can use NCWidgetDisplayMode's second option "compact":

@available(iOS 10.0, *)
public enum NCWidgetDisplayMode : Int {    
    case compact // Fixed height
    case expanded // Variable height
}

you can update your code like below :

if #available(iOSApplicationExtension 10.0, *) {
    self.extensionContext?.widgetLargestAvailableDisplayMode = .compact
}
MGY
  • 7,245
  • 5
  • 41
  • 74
  • this works but i want to change height of widget without using showmore/showless – Noha Mohamed Oct 26 '16 at 13:28
  • @NohaMohamed please check my updated answer. Hope it helps. – MGY Oct 26 '16 at 13:59
  • it works successfully , thanks . Can i hide showmore button and change height ? – Noha Mohamed Oct 27 '16 at 08:33
  • 1
    you can use widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize for change height because compact will make a default (very small) size for your extension. – MGY Oct 27 '16 at 08:48