xib中添加scrollview

http://stackoverflow.com/questions/9118796/steps-for-creating-uiscrollview-with-interface-builder



UPDATE

I have posted another solution here which I think is simpler and better.

ORIGINAL

Here's another way to do this that you might like better:

  1. Set the File's Owner placeholder's custom class to your view controller subclass.
  2. Create the UIScrollView as a top-level object in your nib. Set its size to the screen size (320x460) or just turn on a status bar under "Simulated Metrics".
  3. Connect the scroll view's delegate outlet to File's Owner.
  4. Set the File's Owner's view outlet to the scroll view.
  5. Create a UIView as another top-level object in your nib. This will be your content view.
  6. Set the content view's size to 320x700.
  7. Create a strong (or retain, if not using ARC) outlet named contentView in your view controller (File's Owner) and connect it to the content view.
  8. Put your buttons in the content view.
  9. In your view controller's viewDidLoad, do this:

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.view addSubview:self.contentView];
        ((UIScrollView *)self.view).contentSize = self.contentView.frame.size;
    }
  10. In your view controller's viewDidUnload, do this:

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

scrollbuttons project window
Full size

share | improve this answer
 
 
Thank you! This makes more sense to me... do the "stitching" of the views at run time, so that the building of the content views isn't interfered with by Interface Builder. One question, why set the scroll view size to 460? Is that so that it fulfills the requirement that the scroll view has to be smaller than the 480? –  steve8918 Feb 2 '12 at 20:36
 
The status bar takes 20 points. The size doesn't really matter much, because UIViewController should resize it to be full-screen anyway. –  rob mayoff Feb 2 '12 at 20:39
 
great answer +1 –  Dilip Feb 5 '13 at 12:38
 
For scrollview, Interface builder has wasted hours and hours of my time. It would have been faster to build it in code myself. Most of the time it's my fault, but in this case IB has some issues with throwing everything off by the height of a uinavigation bar. I'm confident that I'm doing it right. I like the stitching things up with code. –  user798719 Jul 23 '13 at 3:50
 
Why is it necessary to set contentView to nil in viewDidUnload? –  Brad Thomas May 20 at 13:40

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值