如何在iOS中使用Storyboard创建居中的ImageView?

5

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


3个回答

5

非常容易...

1.将UIImageView放置在中心

然后添加以下限制

1.水平居中于视图&&垂直居中于视图

2.将其设置为纵横比

请参见下面提供的图片。 enter image description here

enter image description here

enter image description here

设置水平居中、垂直居中和纵横比,您就可以得到...


不行。显示红色标记,如果我清除了那个错误,它又显示不正确。@Kishore! - android
是的,对于iPad来说它显示得很小。但是相比iPhone,iPad应该显示更大的尺寸。现在从你的解决方案中,iPhone在4到5.5英寸的屏幕上显示得很完美。我需要iPad也能够如此显示。@KishoreKumar - android
是的!我已经创建了1x、@2x、3X..@Kishore大小的图像。 - android
然后根据图像设置比例。@android - Kishore Kumar
太棒了!非常感谢你的帮助。愿上帝保佑你!@Kishore和所有的帮手们! - android
显示剩余3条评论

2
你可以尝试使用尺寸类别来适配多种不同的屏幕尺寸。

将约束条件改为这样或其他方式


我仅使用大小类。所有布局的基本值我都在使用。@Klone - android
更改Android中的约束条件。 - klone

0

如果您想在iOS中设计通用应用程序,则必须基于百分比(%)添加约束条件。有一些情况或情况,您无法从Starboard添加约束条件,但不要担心,我们可以通过编程来实现。

您是否尝试过{{link1:KVConstraintExtensionsMaster}}库来应用我已经实现的约束条件?该库具有方法,可以更新应用访问删除应用的约束条件,无论是通过编程方式还是从界面生成器(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. 将imageView拖放到ViewController的视图中。

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

步骤3. 添加等宽度和等高度约束。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接