iOS aspect ratio constraint breaks on iOS7, works on iOS8

前端 未结 6 1748
春和景丽
春和景丽 2021-02-04 18:37

In my app, I have setup a UIView\'s constraints in a way where its height gets calculated from its width using the aspect ratio constraint. It works perfectly on iOS8 with every

相关标签:
6条回答
  • 2021-02-04 19:20

    Indeed, this must be OS (iOS 7) bug. I've spent few days debugging autolayout collapsing totally (note: NO NSLayout exceptions). I've almost collapsed too.

    It was ALL due to using some combinations of constraints together with aspect-ratio constraints.

    I am solving this with redesigning UI...Pain.

    0 讨论(0)
  • 2021-02-04 19:21

    I have this problem with NSLayoutConstraint and multiplier on iOS 7. Works perfectly on iOS 8.

    I'm using multiplier with value 0.2 and it causes errors!

    NSLayoutConstraint(item: BT_Facebook,
                  attribute: NSLayoutAttribute.Width,
                  relatedBy: .Equal,
                     toItem: nil,
                  attribute: .Width,
                 multiplier: 0.2,
                   constant: sizeBtRedeSociais)
    

    The solution is to use the size of the screen, and not percentages.

    var sizeBtRedeSociais = UIScreen.mainScreen().bounds.width / 5
    var BT_FacebookWidth = NSLayoutConstraint(item: BT_Facebook, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: sizeBtRedeSociais)
    var BT_TwitterWidth = NSLayoutConstraint(item: BT_Twitter, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: sizeBtRedeSociais)
    var BT_InstagramWidth = NSLayoutConstraint(item: BT_Instagram, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: sizeBtRedeSociais)
    var BT_GoogleWidth = NSLayoutConstraint(item: BT_Google, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: sizeBtRedeSociais)
    var BT_YoutubeWidth = NSLayoutConstraint(item: BT_Youtube, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: sizeBtRedeSociais)
    
    0 讨论(0)
  • 2021-02-04 19:26

    I resolved it in the following way:

    Assuming my panel will look the same on iPhone 4, 4s, 5, 5s since they have the same width, so their aspect ratio will result in the same height. I added a height constraint to the same view that I added the aspect ratio constraint to. So regardless of the iOS version, on these devices, the view will look right with the height constraint.

    iPhone6 and 6+ only run on iOS8 or higher, where the aspect ratio constraint doesn't fail, I simply delete the height constraint if the iOS version is 8 or higher, else I delete the aspect ratio constraint.

    It's far from being elegant, but solves the issue in an easy way.

    0 讨论(0)
  • 2021-02-04 19:31

    I resolved it You don't set ratio itself. You should set equal with its container But better you should set height constraint follow iPhone 6. You must outlet constrain in your class. In viewDidLoad you must multiplier with DISPLAY_SCALE

     DISPLAY_SCALE = UIScreen.mainScreen().bounds.width / 375
    

    // 375 is width of iPhone 6.

    Why do you must do that? I think iOS7 delay when calculator our constraint. And it wrong when show

    0 讨论(0)
  • 2021-02-04 19:34

    This is one annoying bug, cost me a lot of hours as well. I had the exact same problem, the app was working fine in iOS 8 but crashed in iOS 7.1. The problem was an aspect ratio constraint on a scroll view. After trying out a lot of different things, this finally solved the problem:

    The crash seems to be the result of a floating point calculation that fails at some point. In my case, I had defined an aspect ratio of 16:9 or rather 9:16, which amounts to 0.5625. Changing the multiplier from 0.5625 to 0.5624 fixed the problem, no more crashes have appeared since then.

    0 讨论(0)
  • 2021-02-04 19:35

    I had this crash on iOS7 when I had a combination of aspect ratio, equal height to container, and zero top and bottom spacing to container constraints. The equal height to container constraint was redundant as it was constrained to the top and bottom of the container so I removed it and the crash stopped. It must be a bug in the OS.

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