2010-06-08 40 views
38

Được rồi, do đó, khóa ở đây là tôi không sử dụng IB chút nào, vì Chế độ xem tôi đang làm việc được tạo theo chương trình. Các UIView bao gồm nửa dưới màn hình, và có một loạt các nút trên đó. Tuy nhiên, tôi muốn thêm nhiều nút hơn vào UIView, mà không làm cho nó lớn hơn. Để làm như vậy, tôi muốn tạo một số UIScrollView bên trong chế độ xem, điều này sẽ cho phép tôi thêm nhiều nút hơn vào màn hình để người dùng có thể cuộn đến chúng. Tôi nghĩ đó là cách nó hoạt động.Làm cách nào để tạo UIScrollView theo lập trình?

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

Phần đầu tiên của mã iniates tôi UIView, mà hoạt động tuyệt vời, nhưng tôi không thể tìm ra cách để làm cho UIScrollView lập trình và thêm nó vào xem, và sau đó thêm các nút để nó.

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

Khi tôi thực hiện điều đó, nút chỉ đơn giản biến mất khỏi màn hình của tôi. Vì vậy, làm thế nào để tôi làm điều này một cách chính xác? Cảm ơn sự giúp đỡ của bạn!

Trả lời

26

Thay vì:

UIScrollView *scroll = [UIScrollView alloc]; 

làm điều này (thiết lập các khung để tuy nhiên lớn bạn muốn xem di chuyển được):

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

Ahhh, tôi phải có initwithframe AND .contentSize để nó hoạt động. Cảm ơn bạn! –

23

Điều này có thể hữu ích cho bạn để tạo UIScrollView lập trình
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

Tôi tin rằng bạn cũng cần phải làm '[self.view addSubview: scrollview];' để nó hoạt động. – futurevilla216

+0

@LennyK dường như nó đã được chỉnh sửa theo cách này –

12

thử này,

{ 

     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

Tôi đã sử dụng tùy chọn cuối cùng mà NANNAV trả lời có vẻ như nó hoạt động tốt nhưng bây giờ tôi có thể thấy thanh cuộn nhưng giao diện người dùng của tôi bị khóa. Có nghĩa là điều khiển duy nhất hoạt động là UIScrollView ... bất kỳ ý tưởng nào ... – logixologist

0

Phương pháp đơn giản: Bạn có thể tạo nhiều lần nếu bạn cần thêm

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

tôi sử dụng lazy rất nhiều khi tạo giao diện người dùng lập trình, như thế này:

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

Tạo 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]; 
Các vấn đề liên quan