我是iOS开发的新手,但UIScrollView有问题。首先,我在情节提要中创建了一个Scroll View并添加了

@property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;

在视图控制器处理程序中

viewDidLoad方法中,我有以下代码:
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
    // You should set these.
    self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    self.mainScroll.minimumZoomScale = .5;  // You will have to set some values for these
    self.mainScroll.maximumZoomScale = 2.0;
    self.mainScroll.zoomScale = 1;

    UIButton* b = [[UIButton alloc] init];
    [b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
    [self.mainScroll addSubview:b];
}

但是该按钮未出现在模拟器中。但是,如果我在IB的情节提要板上添加按钮,该按钮就会出现,并且我可以滚动视图。

如何在代码中添加按钮?

最佳答案

试试这个代码

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];

10-04 13:00