如何将按钮和图像放在UIScrollView中

时间:2012-10-14 05:59:17

标签: ios uiscrollview uiimageview uibutton

我在xcode 4中制作了一个ios应用,需要制作一个包含按钮和UIScrollView的{​​{1}}。有谁知道如何编码?

(如果可能的话,我希望能够在故事板中这样做)

谢谢

1 个答案:

答案 0 :(得分:9)

ViewDidLoad方法:

//添加ScrollView

UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)]; 
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);//You Can Edit this with your requirement 

//添加按钮

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self  action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[scrollview addSubview:button];

//添加ImageView

UIImageView*   iv =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourPhoto.png"]];
iv.frame=CGRectMake(0, 0, 40,60);
[scrollview addSubview:iv];