简体   繁体   English

如何使用iOS在故事板上创建ImageView中心?

[英]How to create ImageView center on storyboard using iOS?

I am trying to create Universal application using iOS storyboard within Xcode 7 and iOS 9. In the storyboard concept I can see lots of methods (autolayout, auto constraints, adaptive UI, etc.). 我正在尝试使用Xcode 7和iOS 9中的iOS故事板创建通用应用程序。在故事板概念中,我可以看到许多方法(自动布局,自动约束,自适应UI等)。 I am confused by those methods of design. 我对这些设计方法感到困惑。 Please give me a solution for image view show on center for all iPhone and iPad. 请给我一个解决方案,为所有iPhone和iPad的中心图像视图显示。

Its Quite easy .. 很容易..

1.place the uiimageview to center 1.将uiimageview置于中心位置

Then add the Below constraints 然后添加Below约束

1.centre horizontal to the view && centre vertical to the view 1.centre水平到视图&&中心垂直于视图

2.set it to aspect ratio 2.将其设置为宽高比

See the image given below. 请参见下面的图片。 在此输入图像描述

在此输入图像描述

在此输入图像描述

centre horizontal ,centre vertical ,aspect ratio set this three you will get ... 中心水平,中心垂直,纵横比设置这三个你会得到...

You can try size classes work with many different screen sizes. 您可以尝试使用许多不同屏幕尺寸的尺寸类。

Change constraint like this or other 改变这样或那样的约束

If you want to design Universal application in iOS then you must add constraints base on the percentage(%) and There are few situations or cases for which you can not added the constraints from the Starboard but don't wary about that we can do that programatically. 如果你想在iOS中设计通用应用程序,那么你必须根据percentage(%)添加约束。很少有情况或情况你不能从Starboard添加约束但不要担心我们可以做到这一点编程。

Have you ever try KVConstraintExtensionsMaster library to apply constraints that I have implemented. 你有没有尝试过KVConstraintExtensionsMaster库来应用我已经实现的约束。 This library having the method that will update , apply , access and remove the constrains those are applied either applied by Programmatically or from Interface Builder(StoryBoard/xib) . 该库具有updateapplyaccessremove通过Programmatically或从Interface Builder(StoryBoard/xib)应用的约束的方法。

Programmatically imageView show on center for all iPhone and iPad. 以编程方式在所有iPhone和iPad的中心上显示imageView。

- (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];
     */
}

From StoryBoard imageView show on center for all iPhone and iPad. 从StoryBoard imageView显示在所有iPhone和iPad的中心。

Step 1. Drag and place imageView in view of ViewController. 步骤1.在ViewController视图中拖放imageView。

Step 2. Now Add constraint Horizontal Center in Container and Vertical center in Container. 步骤2.现在在Container中添加约束Horizo​​ntal Center和在Container中添加Vertical Center。

Step 3. Add constraint Equal Width and Equal Height. 步骤3.添加约束Equal Width和Equal Height。

Step 4. Now update multiplier of Equal Width constraint and Equal Height. 步骤4.现在更新Equal Width约束和Equal Height的乘数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM