1
我只是擺弄AutoLayout(使用代碼),並碰上了一些我不太明白的東西。我做了一個簡單的例子來說明這個問題:UIViews不均勻分佈使用VisualFormat
- (void)viewDidLoad
{
[super viewDidLoad];
//create views
UIView *redView = [UIView new];
redView.backgroundColor = [UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints = NO;
UIView *blueView = [UIView new];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
//add views to viewcontroller's view
[self.view addSubview:redView];
[self.view addSubview:blueView];
//add contraints
NSDictionary *views = NSDictionaryOfVariableBindings(redView, blueView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[redView]-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[blueView]-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[redView]-[blueView]-|" options:0 metrics:nil views:views]];
}
我希望下面的結果:http://i.stack.imgur.com/LCWQP.png
但是,相反它提供了以下結果:http://i.stack.imgur.com/DJK5i.png
現在,我知道這是可以解決的通過使用[NSLayoutConstraint constraintWithItem: attribute: relatedBy: toItem: attribute: multiplier: constant: ]
,但我想知道是否有一個VisualFormat選項,或者有隻是在我的代碼中缺少的東西...
在此先感謝您的幫助在我的學習之旅!
太棒了!正是我在找什麼。感謝您的額外解釋! – MichMich