Xcode 8 UIButtons with constraints not showing up

前端 未结 7 1590
醉梦人生
醉梦人生 2020-12-14 03:30

Everything used to work great until yesterday I updated Xcode to version 8.

If I don\'t use any constraints, I can see the buttons but when I apply constraint(s) to

相关标签:
7条回答
  • 2020-12-14 03:54

    Solved this by forcing layout before setting any layout-related properties like clipToBounds etc... The tricky part is where and when to call layoutIfNeeded.

    In my case, I have an UIButton with horizontal,top,and height constraints. Apparently on Xcode 8, my button's title went suddenly missing. Then weird enough the title appeared after I removed the height constraint. This behaviour indicates an issue probably within the view's layout cycle. I needed to have the height constraint so removing it wasn't really the solution. So I tried to call layoutIfNeeded prior to setting the constraints but no effect.

    I generate this button on viewDidLoad on a controller via an NSObject class which handles it. The solution for me was forcing a layoutIfNeeded before calling the method that setUps the button.

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.view layoutIfNeeded];
        //editBooking - NSObject class where the button gets configured.
        [_editBooking setUpViews];
    }
    

    Related issues: clipsToBounds causes UIImage to not display in iOS10 & XCode 8

    Edit:

    "I think the (0, 0, 1000, 1000) bound initialization is the new way Xcode instanciates views from IB. Before Xcode8, views were created with their configured size in the xib, then resized according to screen just after. But now, there is no configured size in IB document since the size depends on your device selection (at the bottom of the screen)."

    Reference: Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews

    From Apple release notes:

    Sending layoutIfNeeded to a view is not expected to move the view, but in earlier releases, if the view had translatesAutoresizingMaskIntoConstraints set to NO, and if it was being positioned by constraints, layoutIfNeeded would move the view to match the layout engine before sending layout to the subtree. These changes correct this behavior, and the receiver’s position and usually its size won’t be affected by layoutIfNeeded.

    Some existing code may be relying on this incorrect behavior that is now corrected. There is no behavior change for binaries linked before iOS 10, but when building on iOS 10 you may need to correct some situations by sending -layoutIfNeeded to a superview of the translatesAutoresizingMaskIntoConstraints view that was the previous receiver, or else positioning and sizing it before (or after, depending on your desired behavior) layoutIfNeeded.

    Reference: iOS10 release notes

    0 讨论(0)
  • 2020-12-14 03:57

    I was missing the point of setting UIButton class to my custom class. I checked my custom class and have seen that I set corner radius on awakeFromNib() method. I called layoutIfNeeded() before I set corner radius which was the solution in my case.

    0 讨论(0)
  • 2020-12-14 04:01

    Confirmed this is an issue with XCode 8 currently (As of Oct 19) distributed through App Store. It unnecessarily removes some <rect/> element from the Storyboard/xib files.

    None of the layoutIfNeeded workarounds works for me. I ended up to download XCode 8 Beta 3 which fixed this issue.

    0 讨论(0)
  • 2020-12-14 04:05

    constraints for scrollview should be: top,bottom,leading,trailing

    constraint for contentview in scrollview should be: top,bottom,leading,trailing,fixed height and center x(horizontally in container)

    0 讨论(0)
  • 2020-12-14 04:08

    I don't know if it's a bug, but I solved it adding self.layoutIfNeeded() on awakeFromNib() method. Just in case, I'm subclassing UIButton...

    BUT, if you don't, you can't solve the same problem adding that line of code (at least, you can't at this Xcode version). Using viewDidLayoutSubviews() doesn't work too...

    0 讨论(0)
  • 2020-12-14 04:10

    It seems that Xcode 8.1 Beta 2 (8T46g) fixes the 1000x1000 content size problem (and of course, the auto-layout issues inside table/collection view cells). So, what it works in Xcode 7.x it works without problems in Xcode 8.1 Beta 2.

    0 讨论(0)
提交回复
热议问题