2014-11-03 79 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

相关问题