Autoresizing under iOS 10 doesn't work

徘徊边缘 提交于 2020-01-01 06:15:18

问题


I had a app that works without AutoLayout and Constraints. I only set the autoresizing in the size inspector. Under iOS 9 and earlier works all fine. Under iOS 10 the autoresizing doesn't work and set the size out of the screen. Look at the following picture : Under iOS 10 in the simulator

The same is happened on a iPhone 5 and iPhone 6 Plus. What can I do, that the autoresizing works fine.

Edit 1: Another workaround is to set the storyboard to Xcode 7.x version. You can do this in the File Inspector under Interface Builder Document. To set the option Opens in to the value Xcode 7.x. Answer the following question with save and close. If you now run the project on your iPad or iPhone all view controller with UIScrollView works fine with the auto resizing.

Important: This workaround to perform after each change in the storyboard.

Edit 2: I had make a cross post in the apple developer forum, look here : https://forums.developer.apple.com/message/182716#182716


回答1:


This is a bug in Xcode 8,Just use 7.x version to build your app ,then it will show normally.




回答2:


First, go into the offending storyboard and find the scrollview in question and in attributes uncheck Autoresize Subviews. This will prevent Xcode from automatically undoing everything we're about to do.

Second, and this is the painful part, you need to fix the widths of the views in the scrollview and return them to their proper settings. Just click on the view, find the right hand control knobs, and drag it back inside the scroll view.

In order to make the next step easier, all of the subviews should be in a single content view nested within the scrollview. (The nested content view is also a "best practice" for using auto-sizing auto-layouts in scrollviews, so get used to it.)

Note that if you don't first uncheck Autoresize Subviews then Xcode will kindly rearrange everything again the next time you reopen your storyboard.

Third, you need IBOutlets for your scrollview and your content view in your code.

Finally, add the following to your ViewController...

- (void)viewDidLayoutSubviews
{
    [self.scrollView setAutoresizesSubviews:YES];

    CGRect frame = self.contentView.frame;
    frame.size.width = self.view.frame.size.width - 20;
    self.contentView.frame = frame;

    [super viewDidLayoutSubviews];
}

Basically, we're reenabling autoresize and then setting the width of the content view to be the proper width. Here, I want my content view to be nested within the scroll view with 10px margins (hence the - 20).

The above needs to be done for each offending scrollview in the storyboard.

A little involved, and just a stopgap measure, but faster than doing a brand new layout using autolayout. I was able to patch up a storyboard with about a dozen scenes and VC's in about 15 minutes. It would have taken much, much, much longer to rebuild everything using auto layout.

Note that this is also NOT fixed in the latest Xcode 8.1 beta. I'm pretty sure they're aware of the bug, but I'm also pretty sure this is Apple's way of "encouraging" their developers to start using auto layout.




回答3:


Two possible solutions,

  1. Modify the autoresizingmask like displayed in the image, it's not 100% ideal, because there is no right padding, but it will work.

  1. Set the frame of your textfields, i.e. CGRectMake(padding, y, self.view.frame.size.width-padding*2, height), this is not great as you'll need to do this for each textfield, but again it will work :p.

Unfortunately I'm not sure why the textfield's width goes to 1000+ when you set it up like you have for iOS 10, it might be something to do with the scrollview, I tested it without a scrollview and it worked as expected.




回答4:


You can change Simulated size of view controller from size inspector. Go to size inspector and change simulated size to Free form. You can set width and height of view as you like. To view the screen as it was in iOS 9, change width and height to 600.




回答5:


According to my tests and this thread bug is fixed in Xcode 8.1 Beta 2.



来源:https://stackoverflow.com/questions/39514044/autoresizing-under-ios-10-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!