0
我想要一個imageview出現在底部的中心,我將imageview添加到視圖控制器中的另一個圖像視圖。結果是imageview出現在底部但不完全在中心,任何人都可以建議我的代碼中出現了什麼問題?如何將UIImageView對齊到視圖控制器的中心?
-(void)viewWillAppear:(BOOL)animated
{
UIImageView *imgView=[[UIImageView alloc]init];
[imgView setTranslatesAutoresizingMaskIntoConstraints:NO];
UIImageView *mspimgvw=[[UIImageView alloc]init];
[mspimgvw setTranslatesAutoresizingMaskIntoConstraints:NO];
mspimgvw.image=[UIImage imageNamed:@"msplogo.jpg"];
[self.imgView addSubview:mspimgvw];
[self.view addSubview:imgView];
CGFloat bottom=0;
[imgView addConstraint:[NSLayoutConstraint constraintWithItem:mspimgvw attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:imgView attribute:NSLayoutAttributeBottom multiplier:1 constant:bottom-40]];
[imgView addConstraint:[NSLayoutConstraint constraintWithItem:mspimgvw attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:imgView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:bottom-40]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imgView]|" options:0 metrics: 0 views:dicViews]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imgView]|" options:0 metrics: 0 views:dicViews]];
}
我想要圖像出現在視圖控制器的中心。在我的代碼中更改什麼?
謝謝你幫我除了需要添加屬性:NSLayoutAttributeBottom做出出現在底部的UIImageView。 – StackUser 2014-11-04 08:56:51
是的你是對的,編輯答案,所以你可以現在接受它。 :) – RJiryes 2014-11-04 09:08:21
你可以建議如何實現UITabBar的自動佈局,如果我添加約束我看不到水平線的選項卡bar.my代碼是 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tabbarObj attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; – StackUser 2014-11-04 09:17:16