9

Here is my storyboard

enter image description here

I'm using autolayout, and NOT using size classes.

When I ran it on iPhone 5s, it works fine.(both portrait and landscape) enter image description here

But when I ran it on iPhone 6 plus (portrait), it's not aligning properly. enter image description here

on iPhone 6 plus (landscape), it's worse. enter image description here

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?

Keoros
  • 1,337
  • 2
  • 13
  • 23
  • Are you using auto layout? If so, just set a constraint with the container margin. – Léo Natan Sep 24 '14 at 20:28
  • But what's the margin, it differ on different devices/orientation – Keoros Sep 25 '14 at 02:38
  • I think the system sets the correct margin for each device – Léo Natan Sep 25 '14 at 07:31
  • I didn't overwrite the widgetMarginInsetsForProposedMarginInsets method. The margin is differ on each device as screenshots above showed.(the blue area is the whole view of storyboard) – Keoros Sep 25 '14 at 08:24
  • 2
    i had to use widgetMarginInsets (adding 11 to left margin on iPad) to align my text to the "TestToday" text in your example. it's also been inconsistent over all devices (even orientations). it also depends whether i use the simulator or actual device. very frustrating! – Joris Weimar Sep 25 '14 at 19:06

4 Answers4

9

Setting the edge insets to zero should fix the problem:

- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
    return UIEdgeInsetsZero;
}
Stephen
  • 1,427
  • 1
  • 17
  • 42
5

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.

Simon Fry
  • 66
  • 2
0

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

Zac Altman
  • 1,215
  • 4
  • 25
  • 37
  • 4
    So I have to separately customize 8 values? (iphone 5, iphone 6, iphone 6+, ipad, on both orientation) – Keoros Sep 25 '14 at 02:35
0

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;
}
Maciej Swic
  • 11,139
  • 8
  • 52
  • 68