2010-06-08 170 views
38

好的,所以這裏的關鍵是我沒有使用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]; 

當我這樣做時,按鈕只是從我的屏幕上消失。那麼我如何正確地做到這一點?感謝您的幫助!

回答

26

相反的:

UIScrollView *scroll = [UIScrollView alloc]; 

做到這一點(設置爲不過大,你要滾動視圖是框架):

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

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

23

這可能有助於您編程創建的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

12

試試這個,

{ 

     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

0

簡單的方法: 如果你需要更多

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

我用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 
     } 
    } 
} 
3

通過編程創建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];