6

I have a scroll view in my app, initialized through storyboard, and I am attempting to change the frame. Everything seems to work, but if the frame and content size are accessed just a short time after they are set, but after my method returns, they are different! Here is the code:

CGFloat inputHeight = inputView.frame.size.height;
originalContentHeight = self.scrollableContentView.frame.size.height;
NSLog(@"%f", self.scrollableContentView.frame.size.height-inputHeight);
[self.scrollableContentView setFrame:CGRectMake(self.scrollableContentView.frame.origin.x, self.scrollableContentView.frame.origin.y, self.scrollableContentView.frame.size.width, self.scrollableContentView.frame.size.height-inputHeight)];
NSLog(@"%f", self.scrollableContentView.frame.size.height);
NSLog(@"%@", self.scrollableContentView);

The output of these logs are all as expected, that is:

<UIScrollView: 0x808de00; frame = (0 0; 320 81); ... >

However, if I set a breakpoint in another method, by the time that is called, logging the scrollView shows this:

<UIScrollView: 0x808de00; frame = (0 0; 320 416); ... >

Additionally, the content size changes as such:

(width=320, height=416)

to

(width=320, height=504)

Both of the values seem to be reverting automatically to the values they have when the view is first laid out. I have set breakpoints in the getter and setter of my scroll view, and cannot find any unexpected accesses, so I have concluded that the change must come internally. Is this a justifiable conclusion? What could be going wrong?

4

1 回答 1

7

如果您使用自动布局,您应该更改约束,而不是设置滚动视图的框架。

在此处查看我对类似问题的回答。它有一些链接到如何以编程方式设置约束的示例。

另外,您在哪种方法中设置frameand contentSize?由于您使用的是故事板,因此如果它在其中,viewDidLoad或者viewWillAppear:最好将此代码移到其中viewDidLayoutSubviews

于 2013-07-16T14:11:44.017 回答