2012-12-12 35 views
5

Tôi đang cố tạo chế độ xem sẽ có nhiều điều khiển khác nhau, nhưng tôi muốn chế độ xem chỉ cuộn theo chiều dọc. Tôi đang sử dụng bố cục tự động vì vậy tôi không muốn phải chỉ định kích thước theo cách thủ công (tôi biết tôi có thể làm điều đó, nhưng từ những gì tôi hiểu theo ghi chú phát hành trên UIScrollView bạn không cần phải). Vì vậy, nếu tôi tạo một bộ điều khiển xem đơn giản (không có tệp .xib) và chỉ cần thêm phần sau vào phương pháp init tôi mong rằng các nhãn của tôi sẽ bao bọc (mà chúng làm) và chế độ xem của tôi sẽ cuộn theo chiều dọc, tuy nhiên, đây không phải là trường hợp:Làm cách nào để sử dụng UIScrollView với AutoLayout (iOS6)?

- (id)init { 
    self = [super init]; 
    if (self) { 

     UIScrollView *_scrollView = [UIScrollView new]; 
     [_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

     UILabel *label1 = [UILabel new]; 
     UILabel *label2 = [UILabel new]; 

     [label1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     [label2 setTranslatesAutoresizingMaskIntoConstraints:NO]; 

     [label1 setNumberOfLines:0]; 
     [label2 setNumberOfLines:0]; 

     [label1 setPreferredMaxLayoutWidth:240]; 
     [label2 setPreferredMaxLayoutWidth:240]; 

     NSString *sampleText = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; 
     [label1 setText:sampleText]; 
     [label2 setText:sampleText]; 

     [self.view addSubview:_scrollView]; 
     [_scrollView addSubview:label1]; 
     [_scrollView addSubview:label2]; 

     NSArray *constraints; 
     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]; 
     [self.view addConstraints:constraints]; 

     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]; 
     [self.view addConstraints:constraints]; 

     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label1]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1)]; 
     [_scrollView addConstraints:constraints]; 

     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label2)]; 
     [_scrollView addConstraints:constraints]; 

     constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)]; 
     [_scrollView addConstraints:constraints]; 
    } 

    return self; 
} 

Có một số kết quả từ mã này mà tôi nghĩ là kỳ quặc. Nếu bạn đăng nhập scrollviewheight theo phương pháp viewDidLayoutSubviews (sau cuộc gọi siêu), đây là nơi áp dụng tất cả các ràng buộc, sau đó chiều cao là báo cáo 0. Điều gì cũng lạ là chiều rộng đang báo cáo một số khác với tôi mong đợi. Tôi hy vọng nó sẽ báo cáo chiều rộng của superview do ràng buộc đầu tiên (tôi thậm chí đã cố gắng buộc chiều rộng bên trong ràng buộc đó với một ưu tiên được cho là bắt buộc nhưng nó vẫn không hoạt động).

Như tôi đã hiểu, kích thước nội dung của UIScrollView phải được đặt theo kích thước nội tại của nội dung nhưng điều đó dường như không xảy ra.

Vì vậy, có ai biết điều gì bị thiếu không?

+0

+1 cho nguồn gốc ipsum lorem) – Stas

Trả lời

9

Tôi đã liên hệ với bộ phận hỗ trợ của Apple để nhận câu trả lời cho câu hỏi của tôi. Vấn đề với mã là các đối tượng chứa trong UIScrollView phải được ghim vào cả trên cùng và dưới cùng theo chiều dọc cho nó để xác định kích thước nội dung nội tại của nó. Vì vậy, các dòng mã sau đây

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)]; 
[_scrollView addConstraints:constraints]; 

nên được thay đổi để:

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)]; 
[_scrollView addConstraints:constraints]; 

Họ cũng chỉ ra rằng tôi đã sử dụng từ khóa "mới" trên một số đối tượng mà cơ bản "init" là không phải là bộ khởi tạo được chỉ định. Trong khi đó không có gì để làm với vấn đề cụ thể của tôi, họ đã chỉ ra rằng bạn nên tránh làm điều đó vì đối tượng của bạn có thể ở trạng thái không hợp lệ.

+0

Adam bạn có thể cụ thể hơn về câu trả lời của táo về phương pháp mới - họ có nói rằng chúng ta nên tránh sử dụng nó? – Stas

+2

@Stas http://stackoverflow.com/questions/719877/use-of-alloc-init-instead-of-new-objective-c –

Các vấn đề liên quan