2015-10-20 102 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你再試一次嗎? –

相關問題