Here is my storyboard
I'm using autolayout, and NOT using size classes.
When I ran it on iPhone 5s, it works fine.(both portrait and landscape)
But when I ran it on iPhone 6 plus (portrait), it's not aligning properly.
on iPhone 6 plus (landscape), it's worse.
I know I can use -widgetMarginInsetsForProposedMarginInsets:
to set the margin, but in that case I will need to customize the margin for every device. That would be horrible :(
So is there a way to align the subview to the title less painfully?
Looks like you have to set it manually. You can do this by creating a constraint, then specifying an IBOutlet to it, and setting the constant depending on the device/orientation.
For reference, here are the margins I found you needed:
- 5S - 1 (2px)
- 6 - 1 (2px)
- 6 plus portrait - 5 (15px)
- 6 plus landscape - 34 (102px)
You can find which one you need from the size of the extension view, which is 414 pt for a portrait iPhone 6.
Setting the edge insets to zero should fix the problem:
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
return UIEdgeInsetsZero;
}
You need to customise the widget margins:
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets;
Documentation: https://developer.apple.com/library/ios/documentation/NotificationCenter/Reference/NCWidgetProviding_Protocol/index.html
Fixes for some devices. Requires Ericas UIDevice-Extension
.
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
defaultMarginInsets.bottom = 0;
if ([UIDevice.currentDevice.modelIdentifier containsString:@"iPhone7,1"] && self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
defaultMarginInsets.left += 5;
} else if ([UIDevice.currentDevice.modelIdentifier containsString:@"iPhone7,1"] && self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular) {
defaultMarginInsets.left += 34;
} else {
defaultMarginInsets.left += 1;
}
return defaultMarginInsets;
}
来源:https://stackoverflow.com/questions/26025139/ios-8-today-widget-alignment-issue