2014-11-03 3 views
0

Я хочу, чтобы изображение выглядело в центре внизу, я добавил изображение в другое изображение в представлении контроллера. В результате изображение будет отображаться внизу, но не точно в центре, может ли кто-нибудь предположить, что не так в моем коде ?Как выровнять 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

+0

Да, вы правы, отредактировал ответ, чтобы принять его сейчас. :) – RJiryes

+0

может предложить, как реализовать автоматический макет для UITabBar, если я добавлю ограничение, я не вижу горизонтальную линию вкладки bar.my код: [self.view addConstraint: [Ограничение NSLayoutConstraintWithItem: tabbarObj: NSLayoutAttributeBottom relatedBy: NSLayoutRelationEqual toItem: атрибут self.view: NSLayoutAttributeBottom multiplier: 1 constant: 0]]; – StackUser

Смежные вопросы