How to add UIScrollView to Interface builder?

后端 未结 10 2292
夕颜
夕颜 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 17:57

    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.

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

    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 讨论(0)
  • 2020-12-12 18:05

    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.

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

    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];
        }
    
    0 讨论(0)
  • 2020-12-12 18:05

    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

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

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

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