如何以编程方式创建UIScrollView?

问题描述:

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

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]; 

代码的第一部分iniates我UIView,伟大的工程,但我无法弄清楚如何使UIScrollView编程,并将其添加到视图,然后将按钮添加到它。

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]; 

当我这样做时,按钮只是从我的屏幕上消失。那么我如何正确地做到这一点?感谢您的帮助!

相反的:

UIScrollView *scroll = [UIScrollView alloc]; 

做到这一点(设置为不过大,你要滚动视图是框架):

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

啊,我必须有initwithframe AND .contentSize才能使用它。谢谢! – 2010-06-08 15:42:22

这可能有助于您编程创建的UIScrollView
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]; 
+5

我相信你也需要''self.view addSubview:scrollview];'它的工作。 – futurevilla216 2011-09-03 19:02:05

+0

@LennyK显然它已经被编辑过这种方式 – 2013-03-13 16:28:50

试试这个,

{ 

     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]; 
} 
+1

我使用了NANNAV回答的最后一个选项,它看起来很棒,但现在我可以看到一个滚动条,但是我的UI被锁定了。这意味着唯一可行的控件是UIScrollView ...任何想法... – logixologist 2012-11-11 05:14:59

简单的方法: 如果你需要更多

- (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]; 
    } 
} 

我用lazy很多编程方式创建用户界面的时候,这样您可以创建多个时间:

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 
     } 
    } 
} 

通过编程创建UiScrollView

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];