动态设置自动布局以在iOS中滚动视图

时间:2014-10-17 05:25:54

标签: ios objective-c autolayout scrollview

我正在动态创建滚动视图现在我想为其设置自动布局      我实现了以下代码来设置自动布局,但它根本不起作用。

   - (void)viewDidLoad
   {
   [super viewDidLoad];
        self.navigationItem.title=@"Insta SMS Collection";
        scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];


    [self.view addSubview:scrollView];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = @{@"scrollView":scrollView};
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:  
    [scrollView]"
    options:kNilOptions
    metrics:nil
    views:views;
    [self.view addSubview:scrollView];
    }

1 个答案:

答案 0 :(得分:1)

添加水平和垂直自动布局。

[self.view addSubview:scrollView];
NSDictionary *dictScrollConst = NSDictionaryOfVariableBindings(scrlView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrlView]|" options:0 metrics:nil views:dictScrollConst]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrlView]|" options:0 metrics:nil views:dictScrollConst]];

也许这会对你有帮助。