问题
I have this irritating issue with widgets that it trims the content of my view in the landscape mode. I have put the content size statically but it doesn't work. Does Apple restrict the widget to have a specific height in landscape? Apple's interface guidelines says it's not recommended but is it doable?
Edit:
回答1:
There is a height limit for today widgets on iOS. The max height is (screen size) - (notification center UI height). Whatever's left after the notification center draws its UI is left for widgets. That's going to be different in landscape than in portrait.
Unfortunately this limit is not documented, nor is there any way to look it up at run time. If you request a larger size, you'll get something less than you requested, but there's no way to ask what the limit is. [And if anyone from Apple reads this, please see rdar://18408718, "Today extensions have undocumented, hard to discover size limits"]
In some cases the notification center seems to impose a lower height limit. This looks like a bug to me but there's no way around it for now.
回答2:
With Swift 3 & IOS10 : Add this method to your widget controller and you will get the maxsize
@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize){
if (activeDisplayMode == NCWidgetDisplayMode.compact) {
self.preferredContentSize = maxSize;
// hide or show what you want
messageLabel.isHidden = false
textLabel.isHidden = true
}
else {
self.preferredContentSize = CGSize(width: 0, height: (maxSize.height - 100) // personnaly I remove 100 to make it easier fo the user to see all the widget
messageLabel.isHidden = true
textLabel.isHidden = false
}
}
来源:https://stackoverflow.com/questions/28314985/issue-in-widgets-in-landscape-mode