2015-10-20 104 views
1

我的故事板具有包含UIScrollView的单个视图控制器。我想在viewController中以编程方式向scrollview添加一些自定义视图(每个视图将包含图像&标签)。我已经为故事板中的UIScrollView设置了约束,但正在努力添加具有约束的自定义视图。我如何做到这一点,以便它可以出现在所有设备(iPhone/iPad)相同?我尝试像下面,但它似乎并没有工作。如何使用自动布局以编程方式添加自定义视图

在我ViewController.m -

#import "MainViewController.h" 

#define DEFAULT_ROW_HEIGHT 50 

@interface MainViewController() 

@property float topMarginFromUpperView; 

@end 

@implementation MainViewController 

-(void) addCustomRowToView { 
    self.topMarginFromUpperView += DEFAULT_ROW_HEIGHT + 10.0f; 
    UIView *customRow = [[UIView alloc] initWithFrame:self.scrollView.bounds]; 
    customRow.backgroundColor = [UIColor redColor]; 
    [self.scrollView addSubview:customRow]; 

    customRow.translatesAutoresizingMaskIntoConstraints = NO; 

    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:self.topMarginFromUpperView]]; 
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]]; 
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f]]; 
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeHeight multiplier:0.1f constant:0.0f]]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.topMarginFromUpperView = 0.0f; 
    for (NSInteger i =0; i< 10; i++) { 
     [self addCustomRowToView]; 
    } 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

编辑:

与约束环境不断挣扎。应用程序因无效约束而崩溃。尝试如下 -

-(void) setConstraints:(UIView *)customRow { 

    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:self.topMarginFromSuperView]]; 
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]]; 
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f]]; 
    [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:customRow attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeHeight multiplier:0.1f constant:0.0f]]; 
} 

-(void) addCustomRowToView { 
    UIView *customRow = [[UIView alloc]initWithFrame:CGRectMake(DEFAULT_PADDING, self.topMarginFromSuperView, self.view.bounds.size.width - 2*DEFAULT_PADDING, DEFAULT_ROW_HEIGHT)]; 
    customRow.backgroundColor = [UIColor redColor]; 
    customRow.translatesAutoresizingMaskIntoConstraints = NO; 

    [self setConstraints:customRow]; 

    [self.scrollView addSubview:customRow]; 
    self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.topMarginFromSuperView + DEFAULT_ROW_HEIGHT); 
    self.topMarginFromSuperView += DEFAULT_ROW_HEIGHT + DEFAULT_PADDING; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.topMarginFromSuperView = 0.0f; 
    //for (NSInteger i =0; i< 10; i++) { 
     [self addCustomRowToView]; 
    // } 
    } 

@end 
+0

此[链接](http://stackoverflow.com/questions/21934558/how-to-set-subviews-with -uutolayout-in-uiscrollview-programatically)可以帮助你。 –

回答

1

这里是你的问题的解决方案:

- (void)addCustomView 
{ 
    UIView *lastView; 
    for (int i = 0; i<10; i++) 
    { 
     UIView *view = [[UIView alloc] init]; 
     view.backgroundColor = i%2 ? [UIColor redColor] : [UIColor greenColor]; 
     view.translatesAutoresizingMaskIntoConstraints = NO; 
     [self.scrollView addSubview:view]; 
     [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[view]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]]; 
     [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeWidth multiplier:1 constant:-20.0]]; 

     if (i == 0) 
     { 
      [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]]; 

     } 
     else 
     { 
      [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lastView(40)]-10-[view(40)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastView,view)]]; 
     } 
     lastView = view; 
    } 
    [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lastView(40)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastView)]]; 
} 
+0

感谢您的帮助。不幸的是你的解决方案不适合我。获取错误为“因未捕获的异常终止应用程序'NSInvalidArgumentException',原因:'无法解析约束格式: 遇到的名称为”width“的度量标准,但未在度量标准或视图字典中指定值 H:| [view(width )] | “ – Nuibb

+0

那么你需要用一个整数值来改变”width“/”height“。 –

+0

@Nuibb你再试一次吗? –

相关问题