Autoresizing under iOS 10 doesn't work

别等时光非礼了梦想. 提交于 2019-12-03 16:22:46

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

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.

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.

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.

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

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