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

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

问题描述

我知道我错过了一些东西,因为这必须很容易实现.

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

我的问题是我的加载屏幕"(在启动后立即出现的屏幕)中有一个 UIImageView,其中有两个不同的图像,分别用于 3.5" 和 4" 尺寸的屏幕.在该图像的某个位置,我放置了一个 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.

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

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 插座,并将插座连接到您的 xib 或故事板中的活动指示器的 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;
}

当然,您需要输入适当的值而不是 200100.您可能想要定义一些命名常量.另外,不要忘记调用[super 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 Autolayout - 如何在视图之间设置两个不同的距离,取决于屏幕高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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