iOS的自动布局 - 如何在视图之间设置两个不同的距离,取决于屏幕高度 [英] iOS Autolayout - How to set two different distances between views, depends on the screen height

查看:728
本文介绍了iOS的自动布局 - 如何在视图之间设置两个不同的距离,取决于屏幕高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我失去了一些东西,因为这已是一些容易实现的。

I know I'm missing something, because this has to be something easy to achieve.

我的问题是,我在我的加载屏幕(即闪之后出现的一种)与两个不同的图像3.5和4尺寸屏幕的的UIImageView。在某一个地方的形象,我把一只UIActivityIndi​​cator,告诉应用程序加载在后台的东西给用户。那个地方是不是两个图像一样的,因为他们中的一个明显高于其他的,所以我想设置一个自动版式的约束,让我把在不同的高度该活动的指标,取决于如果应用程序是在运行iPhone 5或没有。

My problem is that I have in my "loading screen" (the one that appears right after the splash) an UIImageView with two different images for 3.5" and 4" size screen. In a certain place of that images, I put one UIActivityIndicator, to tell the user that the app is loading something in the background. That place is not the same for both images, because one of them is obviously higher that the other, so I want to set an autolayout constraint that allows me to put that activity indicator at different heights, depends on if the app is running in an iPhone 5 or not.

如果没有自动布局,我想设置视图以300 frame.origin.y(例如),然后在ViewController中的viewDidLoad方法,我会问,如果应用程序是在iPhone 5上运行,所以我会改变的值,例如,350。我不知道如何做到这一点使用自动布局,我认为它必须是pretty简单。

Without Autolayout, I'd set the frame.origin.y of the view to 300 (for example), and then in the viewDidLoad method of the ViewController, I'd ask if the app is running in an iPhone 5, so I'd change the value to, for example, 350. I have no idea how to do this using Autolayout and I think it has to be pretty simple.

推荐答案

您可以在您的视图控制器上创建一个 NSLayoutConstraint 插座和插座连接到活动的指标的Y约束在厦门国际银行或故事板。然后,添加一个 updateViewContraints 方法到您的视图控制器和更新约束的不断根据屏幕尺寸。

You can create an NSLayoutConstraint outlet on your view controller and connect the outlet to the activity indicator's Y constraint in your xib or storyboard. Then, add an updateViewContraints method to your view controller and update the constraint's constant according to the screen size.

下面是一个例子 updateViewConstraints

- (void)updateViewConstraints {
    [super updateViewConstraints];
    self.activityIndicatorYConstraint.constant =
        [UIScreen mainScreen].bounds.size.height > 480.0f ? 200 : 100;
}

当然,你会希望把你适当的值,而不是 200 100 。您可能要定义一些命名常量。另外,不要忘记调用 [超级updateViewConstraints]

Of course you will want to put in your appropriate values instead of 200 and 100. You might want to define some named constants. Also, don't forget to call [super updateViewConstraints].

这篇关于iOS的自动布局 - 如何在视图之间设置两个不同的距离,取决于屏幕高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆