UIScrollView when Autolayout is turned on (StoryBo

2019-08-30 17:11发布

问题:

I was wrestling with UIScrollview and it knocked me out. I tried to take reference from following two post on stackOverFlow

scrollview xcode4.5 autolayout, Xcode 4.5 UIScrollView, Embed ImageView in ScrollView with Auto Layout on iOS 6,

I tried viewDidLayoutSubviews, as told in UIScrollView setContentSize breaks view with Auto Layout that is causing my app to crash.

And I tried ViewDidLoad, viewDidAppear and viewWillAppear but nothing worked. I tried google and found couple more related post https://coderwall.com/p/zfe8da, http://www.devx.com/wireless/Article/45113

I tried to rethink and didnt find anything fancy that I was not doing. It seems simple, straight approach.

I am out of clues or any guess please help me.

I am using following code, just to mentioned I have 3 images that I have put in the ScrollView. I have stack them on top of each other so there height is more than iPhone screen height. I was expecting it I set them just scroll simple, but guess I was wrong. In Implementation file

_howtoScrollView.frame = CGRectMake(0, 0, 320, 460);
_howtoScrollView.scrollEnabled = YES;
[self.howtoScrollView setContentSize:CGSizeMake(self.howtoScrollView.frame.size.width, 700)];
[self.view addSubview:_howtoScrollView];

In interface file .h

@property (weak, nonatomic) IBOutlet UIScrollView *howtoScrollView;

Anyone who got success with ScrollView while having StoryBoard Autolayout.

回答1:

Try removing _howtoScrollView.frame = CGRectMake(0, 0, 320, 460);, see if it works.

If you really need to change the frame of a view, in Autolayout, you should not set the frame directly in your code, you should modify the Constraint instead.



回答2:

Try to do it without the "addSubview" function. I think that with AutoLayout override your other settings...



回答3:

Using interface builder,

expand your scroll view's Constraints section. Then select 'Horizontal Space Constraint'. Under that section you must be probably having a negative value for the 'constant' value. Change it to 0. It will do the trick.



回答4:

I had this problem too. Apple hid the explanation in the release notes. http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0. Look under the UIKit section.