【问题标题】:not able to display controls add in scroll view's content view using auto layout无法显示控件使用自动布局添加到滚动视图的内容视图中
【发布时间】:2013-10-18 12:34:56
【问题描述】:

我是自动布局部分的新手。我正在以编程方式创建一个带有滚动的视图,但我无法在滚动视图的内容视图中添加控件

这是我的代码:

-(void)setConstraints
{

    UIScrollView *scrollView = [UIScrollView new];
    [scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
    scrollView.backgroundColor = [UIColor greenColor];

    UIView *contentView = [UIView new];
    [contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
    contentView.backgroundColor = [UIColor redColor];

    UILabel *lableTitle = [UILabel new];
    lableTitle.text = @"Registration";
    [lableTitle setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSLayoutConstraint *layoutLableTop = [NSLayoutConstraint constraintWithItem:lableTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:20];
    [self.view  addConstraint:layoutLableTop];

    NSLayoutConstraint *layoutLableCenterX = [NSLayoutConstraint constraintWithItem:lableTitle attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [self.view addConstraint:layoutLableCenterX];

    [contentView  addSubview:lableTitle];
    [scrollView addSubview:contentView];

    [self.view addSubview:scrollView];


}

显示的视图只是白屏。我做错了什么?

【问题讨论】:

    标签: iphone ios ipad nslayoutconstraint autolayout


    【解决方案1】:

    例如,您必须为尺寸添加约束

    [self.view addConstraint:
     [NSLayoutConstraint constraintWithItem:scrollView
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:contentView
                                  attribute:NSLayoutAttributeWidth
                                 multiplier:1
                                   constant:200]];
    [self.view addConstraint:
     [NSLayoutConstraint constraintWithItem:scrollView
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:contentView
                                  attribute:NSLayoutAttributeHeight
                                 multiplier:1
                                   constant:200]];
    

    【讨论】:

    • 对不起@jaba,但它显示了视图。
    • 你在哪里调用方法?
    • 它应该显示 300-300 大小的绿色视图。
    • 是的,它现在正在显示滚动视图@jaba。但我仍然需要做更多的事情。之后我会更新你。如果我找不到路。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多