如何以编程方式创buildUIScrollView?

好的,所以这里的关键是我没有使用IB,因为我正在使用的视图是以编程方式创build的。 UIView覆盖屏幕的下半部分,并在其上有一堆button。 但是,我想添加更多的button到UIView ,而不是更大。 为此,我想在视图中创build一个UIScrollView ,这将允许我在屏幕上添加更多button,以便用户可以滚动到它们。 我认为这是如何工作的。

 self.manaView = [[[UIView alloc] initWithFrame:frame] autorelease]; self.manaView.backgroundColor = [UIColor purpleColor]; UIScrollView *scroll = [UIScrollView alloc]; scroll.contentSize = CGSizeMake(320, 400); scroll.showsHorizontalScrollIndicator = YES; [self.manaView addSubview:scroll]; 

代码的第一部分介绍了我的UIView ,这很好,但我不知道如何编程UIScrollView并将其添加到视图,然后将button添加到它。

 UIButton *ret2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; ret2.tag = 102; ret2.frame = CGRectMake(255, 5, 60, 50); [ret2 setTitle:@"Return" forState:UIControlStateNormal]; [ret2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside]; [scroll addSubview:ret2]; 

当我这样做,button简单地消失在我的屏幕上。 那么我如何正确地做到这一点? 感谢您的帮助!

代替:

 UIScrollView *scroll = [UIScrollView alloc]; 

做到这一点(设置框架,不pipe你想要滚动视图):

 UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...]; 

这可能有助于您以编程方式创builduiscrollview
http://unconditionalloop.blogspot.com/2011/04/uiscrollview-programmatically.html

  UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; NSInteger viewcount= 4; for(int i = 0; i< viewcount; i++) { CGFloat y = i * self.view.frame.size.height; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)]; view.backgroundColor = [UIColor greenColor]; [scrollview addSubview:view]; [view release]; } scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount); [self.view addSubview:scrollview]; 

尝试这个,

 { 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(width,height); [scrollview release]; } 

创build一个UiScrollView Programitically

 ScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)]; ScrView.showsVerticalScrollIndicator=YES; [ScrView setBackgroundColor:[UIColor yellowColor]]; [ScrView setScrollEnabled:YES]; [ScrView setContentSize:CGSizeMake(0, 700)]; [ScrView setShowsHorizontalScrollIndicator:YES]; [ScrView setShowsVerticalScrollIndicator:YES]; [self.view addSubview:ScrView]; 

简单的方法:你可以创build多次,如果你需要的手段

 - (void)viewDidLoad { [super viewDidLoad]; int x = 0; int y = 10; for(int i=0; i<5; i++) { UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)]; scrollview.showsVerticalScrollIndicator=YES; scrollview.scrollEnabled=YES; scrollview.userInteractionEnabled=YES; scrollview.backgroundColor = [UIColor greenColor]; [self.view addSubview:scrollview]; //scrollview.contentSize = CGSizeMake(50,50); x=x+55; //[self myscrollView]; } } 

以编程方式创buildUI时,我使用了很多lazyfunction,如下所示:

 class WorldViewController: UIViewController { override func loadView() { super.loadView() view = scrollView scrollView.addSubview(label0) } lazy var scrollView: UIScrollView = { let instance = UIScrollView() instance.backgroundColor = UIColor.blackColor() return instance }() lazy var label0: UILabel = { let instance = UILabel() instance.text = "Ice caps are melting" instance.textColor = UIColor.whiteColor() instance.sizeToFit() return instance }() var needLayoutContent = true override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() if needLayoutContent { let bounds = scrollView.bounds let contentSize = CGSizeMake(bounds.width * 1.5, bounds.height * 1.5) label0.center = CGPointMake(contentSize.width / 2, contentSize.height / 2) scrollView.contentSize = contentSize scrollView.contentOffset = CGPointMake(bounds.width * 0.25, bounds.height * 0.25) needLayoutContent = false } } }