2016-02-23 50 views
1

我在ViewController中有两个标签,其中一个标签会动态改变,因为数据长度不同(我从JSON文件获取数据)。 我试过使用 self.automaticallyAdjustsScrollViewInsets = NO; 但不起作用。 我添加了ScrollView到我的ViewController。ScrollView在ViewController中不起作用

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.automaticallyAdjustsScrollViewInsets = NO; 
NSLog(@"hjgjgjk==%@",detailArray); 

NSDictionary *dictionary = [detailArray mutableCopy]; 
expressionLabel.text = [dictionary objectForKey:@"expression"]; 

meaningLabel.text = [dictionary objectForKey:@"meaning"]; 

你能解释一下我该走哪条路吗?

回答

1

我希望你已经在scrollview上添加了标签。

添加文本到标签后请在下面添加行。

self.automaticallyAdjustsScrollViewInsets = NO; 

而你的情况必须

NSLog(@"hjgjgjk==%@",detailArray); 

NSDictionary *dictionary = [detailArray mutableCopy]; 
expressionLabel.text = [dictionary objectForKey:@"expression"]; 

meaningLabel.text = [dictionary objectForKey:@"meaning"]; 

self.automaticallyAdjustsScrollViewInsets = NO; 

我这样的建议是象下面这样:

UIScrollView *scrollView= [UIScrollView new]; 
    scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.view addSubview:scrollView]; 

    UILabel *scrollViewLabel = [[UILabel alloc] init]; 
    scrollViewLabel.numberOfLines = 0; 
    scrollViewLabel.translatesAutoresizingMaskIntoConstraints = NO; 
    [scrollView addSubview:scrollViewLabel]; 

    scrollViewLabel.text = @"Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world."; 


    /*** Auto Layout ***/ 

    NSDictionary *views = NSDictionaryOfVariableBindings(scrollView, scrollViewLabel); 

    NSArray *scrollViewLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollViewLabel(scrollView)]" options:0 metrics:nil views:views]; 
    [scrollView addConstraints:scrollViewLabelConstraints]; 

    scrollViewLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollViewLabel]|" options:0 metrics:nil views:views]; 
    [scrollView addConstraints:scrollViewLabelConstraints]; 

    NSArray *scrollViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[scrollView]-|" options:0 metrics:nil views:views]; 
    [self.view addConstraints:scrollViewConstraints]; 

    scrollViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[scrollView]-|" options:0 metrics:nil views:views]; 
    [self.view addConstraints:scrollViewConstraints]; 

对于两个(或多个视图)标签设置水平:

UIScrollView *scrollView= [UIScrollView new]; 
    scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.view addSubview:scrollView]; 

    UILabel *scrollViewLabel = [[UILabel alloc] init]; 
    scrollViewLabel.numberOfLines = 0; 
    scrollViewLabel.translatesAutoresizingMaskIntoConstraints = NO; 
    [scrollView addSubview:scrollViewLabel]; 

    UILabel *scrollViewLabel2 = [[UILabel alloc] init]; 
    scrollViewLabel2.numberOfLines = 0; 
    scrollViewLabel2.translatesAutoresizingMaskIntoConstraints = NO; 
    [scrollView addSubview:scrollViewLabel2]; 

    scrollViewLabel.text = @"Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.Hello world."; 


    scrollViewLabel2.text = @"Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there.Hi there."; 

    /*** Auto Layout ***/ 

    //scrollviewLabel 
    NSDictionary *views = NSDictionaryOfVariableBindings(scrollView, scrollViewLabel, scrollViewLabel2); 

    NSArray *scrollViewLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollViewLabel(scrollView)]" options:0 metrics:nil views:views]; 
    [scrollView addConstraints:scrollViewLabelConstraints]; 

    scrollViewLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollViewLabel]|" options:0 metrics:nil views:views]; 
    [scrollView addConstraints:scrollViewLabelConstraints]; 


    //scrollviewLabel2 
    NSArray *scrollViewLabel2Constraints = [NSLayoutConstraint 
           constraintsWithVisualFormat:@"|-[scrollViewLabel]-[scrollViewLabel2(==scrollViewLabel)]-|" 
           options:NSLayoutFormatAlignAllBaseline metrics:nil 
           views:views]; 
    [scrollView addConstraints:scrollViewLabel2Constraints]; 


    //scrollview 
    NSArray *scrollViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[scrollView]-|" options:0 metrics:nil views:views]; 
    [self.view addConstraints:scrollViewConstraints]; 

    scrollViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[scrollView]-|" options:0 metrics:nil views:views]; 
    [self.view addConstraints:scrollViewConstraints]; 

希望这会有所帮助。

+0

感谢您的帮助。我在DetailViewController中显示标签时遇到问题,请检查我的代码吗?https://goo.gl/ppA3AV – mechu911

+0

确定我现在检查。 –

+0

和你怎么看? – mechu911

相关问题