繁体   English   中英

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

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

我正在尝试使用Xcode 7和iOS 9中的iOS故事板创建通用应用程序。在故事板概念中,我可以看到许多方法(自动布局,自动约束,自适应UI等)。 我对这些设计方法感到困惑。 请给我一个解决方案,为所有iPhone和iPad的中心图像视图显示。

很容易..

1.将uiimageview置于中心位置

然后添加Below约束

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

2.将其设置为宽高比

请参见下面的图片。 在此输入图像描述

在此输入图像描述

在此输入图像描述

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

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

改变这样或那样的约束

如果你想在iOS中设计通用应用程序,那么你必须根据percentage(%)添加约束。很少有情况或情况你不能从Starboard添加约束但不要担心我们可以做到这一点编程。

你有没有尝试过KVConstraintExtensionsMaster库来应用我已经实现的约束。 该库具有updateapplyaccessremove通过Programmatically或从Interface Builder(StoryBoard/xib)应用的约束的方法。

以编程方式在所有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];
     */
}

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

步骤1.在ViewController视图中拖放imageView。

步骤2.现在在Container中添加约束Horizo​​ntal Center和在Container中添加Vertical Center。

步骤3.添加约束Equal Width和Equal Height。

步骤4.现在更新Equal Width约束和Equal Height的乘数。

暂无
暂无

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

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