How to add UIScrollView to Interface builder?

后端 未结 10 2300
夕颜
夕颜 2020-12-12 17:50

I have all my controls laid out in interface builder (many labels, buttons etc). How do I put them all in a scroll view in interface builder so that I can have more space an

相关标签:
10条回答
  • 2020-12-12 18:08

    Select all the objects you want to put into a scroll view and go to the Layout menu, choose "Embed Objects In" and choose "Scroll View".

    0 讨论(0)
  • 2020-12-12 18:11

    Important little thing. To scroll big subview (UIImageView for example) in UIScrollView remember, for this subview, uncheck "User Interaction Enabled" checkbox in InterfaceBuilder -> View window. Or do it programatically.

    subview.userInteractionEnabled = NO;
    

    Otherwise this subview will stack on screen without any effect.

    0 讨论(0)
  • 2020-12-12 18:13

    Although this question is very old, I will suggest a workaround I found as I had the same issue and wasn't able to find much help out there:

    When in IB, if you want to place objects outside the 420 pixel, just make sure yourself of having selected Unspecified for all of Status Bar, Top Bar, and Bottom Bar for the View that contains the Scroll View with all the objects. This way, you'll be able to manually resize the screen (for the View). Then, you can follow Ximonn's advice on resizing the H value for the Scroll View, having access to all the other objects, working with them and then, undoing changes for H value and then setting the needed Bars.

    0 讨论(0)
  • 2020-12-12 18:16

    My preferred solution, where you don't need to hard-code the size of the contentSize:


    NB: you might be able to avoid the source-code parts of this using the trick here: https://stackoverflow.com/a/11239123/153422 - although I haven't tried it yet.

    The rest of this trick ... you still need to use anyway


    1. Move all controls into a single UIView (in IB: select all, then go Layout > Embed Objects In ... > View)

    2. Hookup that single UIView to your source code using an IBOutlet property (see below)

    3. IN SOURCE CODE, NOT INTERFACE BUILDER (IB is broken here, it has bugs where it sets the origin of the UIScrollView incorrectly - it tries to center the view. Apple never bothered to check it for basic bugs, sigh): Move the single UIView into a UIScrollView (see code below).

    4. Use sizeThatFits to "automatically" set the correct size.

    Code (StackOverflow won't let me put code inside a numbered list. Sigh)

    Header file:

    /** outlet that you hook up to the view created in step 1 */
    @property(nonatomic, retain) IBOutlet UIView *masterView;
    

    Class file:

    /** inside your viewDidLoad method */
    [scrollview addSubview: masterView]; // step 3
    scrollView.contentSize = [masterView sizeThatFits:CGSizeZero]; // step 4
    

    ...although I haven't checked this recently, IIRC it works on both 2.x and 3.x

    0 讨论(0)
提交回复
热议问题