2014-11-03 87 views
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]]; 
} 

我想要圖像出現在視圖控制器的中心。在我的代碼中更改什麼?

回答

1

在這裏你去:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView 
                 attribute:NSLayoutAttributeCenterX 
                 relatedBy:NSLayoutRelationEqual 
                 toItem:self.view 
                 attribute:NSLayoutAttributeCenterX 
                multiplier:1.f 
                 constant:0.f]]; 

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView 
                 attribute:NSLayoutAttributeBottom 
                 relatedBy:NSLayoutRelationEqual 
                 toItem:self.view 
                 attribute:NSLayoutAttributeBottom 
                multiplier:1.f 
                 constant:0.f]]; 
+0

謝謝你幫我除了需要添加屬性:NSLayoutAttributeBottom做出出現在底部的UIImageView。 – StackUser 2014-11-04 08:56:51

+0

是的你是對的,編輯答案,所以你可以現在接受它。 :) – RJiryes 2014-11-04 09:08:21

+0

你可以建議如何實現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

相關問題