从xib添加ScrollView的约束

时间:2016-05-05 02:44:28

标签: ios objective-c uiscrollview autolayout xib

UIScrollViewUIView(作为ContentView)与UITableView和两个UIWebView(请参阅img)。我需要将它放到ViewController。

我现在怎么做:

OrderInfoScrollView *newView = [[OrderInfoScrollView alloc] loadObject];
[self.view addSubview:newView];

和init看起来像:

- (OrderInfoScrollView *)loadObject {
    OrderInfoScrollView *result = nil;
    NSArray* elements = [[NSBundle mainBundle] loadNibNamed: @"OrderInfoView" owner:self options: nil];
    for (id anObject in elements)
    {
        if ([anObject isKindOfClass:[self class]])
        {
            result = anObject;
            break;
        }
    }

    return result;
}

它工作正常,但我无法为此添加约束。我需要equal width to self.view, leading space = 3, top margin = 0

我已经尝试过这个:

UIView *superView = self.OrderInfoView;
[superView addSubview:newView];
NSDictionary *views = NSDictionaryOfVariableBindings(superView, newView);
NSArray *equalWidthConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[newView]" options:0 metrics:nil views:views];
NSArray *verticalConstraints =[NSLayoutConstraint 
//NSArray *equalWidthConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[newView(==superView)]" options:0 metrics:nil views:views];
NSArray *verticalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[newView]" options:0 metrics:nil views:views];
NSArray *horizontalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[newView]" options:0 metrics:nil views:views];

[superView addConstraints:equalWidthConstraints];
[superView addConstraints:horizontalConstraints];
[superView addConstraints:verticalConstraints];

这样的事情:

 NSLayoutConstraint *constraint = [NSLayoutConstraint
                                       constraintWithItem:newView
                                       attribute:NSLayoutAttributeWidth
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:self.view
                                       attribute:NSLayoutAttributeWidth
                                       multiplier:1.0f
                                       constant:0.0f];

[self.view addConstraint:constraint4];

另外,我尝试添加其他UIView并将滚动视图添加到此处(没有帮助)。结构是这样的:

-UIView
--UIView
---UIScrollView

我尝试了setTranslatesAutoresizingMaskIntoConstraints:NO和新方法,但在这种情况下我的UIScrollView根本没有出现。

[newView setTranslatesAutoresizingMaskIntoConstraints:YES];
NSLayoutConstraint *width =[NSLayoutConstraint
                            constraintWithItem:newView
                            attribute:NSLayoutAttributeWidth
                            relatedBy:0
                            toItem:self.view
                            attribute:NSLayoutAttributeWidth
                            multiplier:1.0
                            constant:0];
[self.view addConstraint:width];

有什么想法吗? 我真的需要重用这个UIScrollView

Nib with UIScrollView

1 个答案:

答案 0 :(得分:0)

这是一个很长的路,但我明白了。我为UIView(a.k.a ContentView)创建单独的xib,为所有UIViewsfirst is top margin = 0last one margin bottom = 0设置约束。 setTranslatesAutoresizingMaskIntoConstraints: NO ContentView以及其中的所有观点。以这种方式启动:

OrderInfoView *contentView = [[OrderInfoView alloc] loadObject];
[self.orderInfoScrollView addSubview:contentView];

UIView *superView = self.orderInfoScrollView;
OrderInfoView *newView = contentView;
UIView *megaView = self.view;
NSDictionary *views = NSDictionaryOfVariableBindings(superView, newView, megaView);
NSArray *verticalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[newView]|" options:0 metrics:nil views:views];
NSArray *horizontalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[newView]|" options:0 metrics:nil views:views];

[superView addConstraints:horizontalConstraints];
[superView addConstraints:verticalConstraints];

[newView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *width =[NSLayoutConstraint
                            constraintWithItem:newView
                            attribute:NSLayoutAttributeWidth
                            relatedBy:0
                            toItem:megaView
                            attribute:NSLayoutAttributeWidth
                            multiplier:1.0
                            constant:0];
[megaView addConstraint:width];

希望它对某人有用。