如何使用 iOS 在 Storyboard 上创建 ImageView 中心?

How to create ImageView center on storyboard using iOS?

我正在尝试使用 Xcode 7 和 iOS 9 中的 iOS 故事板创建通用应用程序。在故事板概念中,我可以看到很多方法(自动布局、自动约束、自适应 UI,等等)。我对那些设计方法感到困惑。请为所有 iPhone 和 iPad.

提供中心显示图像视图的解决方案

很简单..

1.place uiimageview 居中

然后添加以下约束

1.centre 水平于视图 && 中心垂直于视图

2.set 长宽比

见下图。

水平居中,垂直居中,纵横比设置这三个你会得到...

您可以尝试尺寸 类 与许多不同的屏幕尺寸一起使用。

像这样或那样改变约束条件

如果你想在 iOS 中设计通用应用程序,那么你必须在 percentage(%) 的基础上添加约束,并且很少有情况或情况你不能从右舷添加约束但是不要担心我们可以通过编程来做到这一点。

你试过 KVConstraintExtensionsMaster 库来应用我已经实现的约束吗?该库具有将 updateapplyaccessremove 约束的方法,这些约束由 ProgrammaticallyInterface Builder(StoryBoard/xib) 应用.

以编程方式 imageView 显示在所有 iPhone 和 iPad 的中心。

- (void)viewDidLoad
{
    [super viewDidLoad];

    /* preparing View Hierarchy */
    UIImageView *imageView = [UIImageView prepareNewViewForAutoLayout];
    [self.view addSubview:imageView];
    [imageView setBackgroundColor:[UIColor blueColor]];

    /* applying constraints */
    [imageView applyConstraintForCenterInSuperview];

    /* Change the Ratio 0.0 to 1.0 to vary the height and width */
    [imageView applyEqualHeightRatioPinConstrainToSuperview:0.8];
    [imageView applyEqualWidthRatioPinConstrainToSuperview:0.8];

    /* Comment the above both RatioPinConstrainToSuperview methods and uncomment below methods and run on both iPhone or iPad and see the magic.*/
    /*
     [imageView applyWidthConstraint:280]; // fixed width for iphone
     [imageView applyHeightConstrain:280]; // fixed height for iphone

     // update width constraint with proper iPad ratio
     [imageView updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeWidth];

     // update height constraint with proper iPad ratio
     [imageView updateAppliedConstraintConstantValueForIpadByAttribute:NSLayoutAttributeHeight];
     */
}

所有 iPhone 和 iPad 的 StoryBoard imageView 显示在中心。

第 1 步。将 imageView 拖放到 ViewController 视图中。

第 2 步。现在添加约束容器中的水平居中和容器中的垂直居中。

第 3 步。添加约束等宽和等高。

第 4 步。现在更新等宽约束和等高约束的乘数。