ScrollView created in storyboard initialized with frame 0,0,0,0

筅森魡賤 提交于 2019-12-19 06:56:26

问题


I created a UIScrollView in the storyboard, linked it as a property to the view controller, and am now trying to add some subviews with pagination. But, even though I set the dimensions in the storyboard graphically, the frame of the UIScrollView ends up being 0,0,0,0. Am I required to hardcode the dimensions even though I already specified them graphically in the storyboard?

Also, kind of related, is it possible to customize subviews in the storyboard to use in this scroll view? It seems that I cannot create such a view unless it is already within a controller. Since I only have one controller and potentially multiple views I want to set up graphically, how do I do this without having to do so programmatically?


回答1:


With storyboards viewDidLayoutSubviews is where you can get the size of all your views. viewWillAppear did not work for me and always returned (0,0,0,0);




回答2:


With storyboards the outlets of a controller are not connected before viewDidLoad and the size of it is not set until viewWillAppear, see here:

http://cs193p.m2m.at/lecture-8/

As soon as the controller is fully instantiated and its outlets are hooked up viewDidLoad: is called. Thus this is a good place to put setup code. However the size of controller is not set yet, but in viewWillAppear: which is called just before the view appears on screen.

So I think Enricos suggestion is correct. Just try putting the size-related stuff into viewWillAppear.




回答3:


I found out that when I access the scrollview in viewWillAppear, the frame will actually give the correct numbers. I previously tried this in awakeFromNib and viewDidLoad and got 0,0,0,0 frame as well.

What's weird is before Storyboard, you are able to get the correct frame dimensions in awakeFromNib. An example of this is in one of Apple's sample code PageControl.

So in short to answer the question, try accessing it from viewWillAppear(Assuming you created the UIScrollView in Storyboard).




回答4:


You probably set the breakpoint on a line where the view was not setup yet. Try this:

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        CGRect frame = self.scrollView.frame;
        NSLog (@"FRAME IS %@", NSStringFromCGRect(frame));
    }
    return self;
}



回答5:


It's quite simple: Disable Use Autolayout in the Storyboard settings.



来源:https://stackoverflow.com/questions/11090999/scrollview-created-in-storyboard-initialized-with-frame-0-0-0-0

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