开发者

How to add UIScrollView to Interface builder?

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 and 开发者_StackOverflow中文版be able to scroll up and down to reveal more controls? Do I have to do this programatically?


Open the view that has all the controls and labels, etc. (in Interface Builder). Select All. Then under the Editor menu, select Embed In, then Scroll View.

Note: in older Xcode versions, this is under the Layout menu, then Embed Objects In... (scroll view).


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


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".


Its easy:

First add a scrollview to your view. Change the size of the scrollview (e.g. make it 700 pixels long). Start putting your controls When you want to put/edit controls in the lower (invisble) part, select the scrollview and change the Y-start position to -300. Voila. After editing set the Y-start position back to 0 or whatever it was.


I don't know if it's just me, but I tried to follow the instructions in each of the other answers here and none of them worked. None of the answers included everything needed, each one I guess assuming we know to do something so leaving that part out. I finally figured it out with the help of red artisan. So... I am listing here ALL the necessary steps to get this to work:

  1. In InterfaceBuilder, add a View and then add your controls to it (or if your controls already exist in the main view, you can select all your controls and then go to Editor | Embed In | View, then drag that new View so it is all by itself outside the main view). This View can be any size you like.

  2. In InterfaceBuilder, add a Scroll View to your main view, and size it to take up the whole main view.

  3. Add the code listed below to your UIViewController Header and Class files.

  4. In InterfaceBuilder, hook up the View containing your controls to 'contentView' in the File's Owner. Hook up the Scroll View to 'scrollView' in the File's Owner.

Header File:

    @interface MyViewController : UIViewController
     @property(nonatomic, retain) IBOutlet UIScrollView *scrollView;  
     @property(nonatomic, retain) IBOutlet UIView *contentView;  

Class File:

    @synthesize scrollView, contentView;

    - (void)viewDidLoad
    {
        [super viewDidLoad];    
        [self.scrollView addSubview:self.contentView];
        self.scrollView.contentSize = self.contentView.bounds.size;
    }

    - (void)viewDidUnload
    {  
        self.scrollView  = nil;
        self.contentView = nil;
        [super viewDidUnload];
    }


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.


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.


I've been looking for this for a few days, and I finally came across this site with a solution that worked for me.

Scrolling with UIScrollView

Basically you have your main view with a UIScrollView object in it. Then another content view with all your content in it. Then you add the content view to the scroll view. And then finally set the size of the scrollview's content size to the size of the content view.


I know, this thread is a bit older... But somebody could find it on google, it's hight ranked. I wrote this little helper Method to get the job done:

- (void)addSubview:(UIView *)theSubView toScrollView:(UIScrollView *)theScrollView
{
    [theScrollView addSubview:theSubView];
    theScrollView.contentSize = theSubView.bounds.size;
}

You just have to declare two IBOutlet's in the header (e.g. contentView and scrollView) and call the method like this, whereever you want to load a UIView into a UIScrollView with your sourcecode:

[self addSubview:contentView toScrollView:scrollView];

I called it in viewDidLoad

This method features iOS


The selected answer works well for Xcode 3.

However, for Xcode 4, menus have been re-arranged slightly.

To do the same in Xcode 4 select your views then use:

Editor > Embed In > Scroll View

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜