iOS Autolayout - 如何在视图之间设置两个不同的距离,取决于屏幕高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15744287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Autolayout - How to set two different distances between views, depends on the screen height
提问by Daniel
I know I'm missing something, because this has to be something easy to achieve.
我知道我错过了一些东西,因为这必须很容易实现。
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.
我的问题是我的“加载屏幕”(在启动后立即出现的屏幕)中有一个 UIImageView,其中有两个不同的图像,分别用于 3.5" 和 4" 尺寸的屏幕。在该图像的某个位置,我放置了一个 UIActivityIndicator,以告诉用户该应用程序正在后台加载某些内容。两个图像的那个位置不同,因为其中一个明显高于另一个,所以我想设置一个自动布局约束,允许我将该活动指示器放在不同的高度,这取决于应用程序是否在iPhone 5 与否。
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.
如果没有自动布局,我会将视图的 frame.origin.y 设置为 300(例如),然后在 ViewController 的 viewDidLoad 方法中,我会询问该应用程序是否在 iPhone 5 中运行,所以我d 将值更改为,例如,350。我不知道如何使用 Autolayout 做到这一点,我认为它必须非常简单。
回答by rob mayoff
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.
您可以NSLayoutConstraint
在视图控制器上创建一个插座,并将插座连接到您的 xib 或故事板中活动指示器的 Y 约束。然后,updateViewContraints
向视图控制器添加一个方法并根据屏幕大小更新约束的常量。
Here's an example of updateViewConstraints
:
下面是一个例子updateViewConstraints
:
- (void)updateViewConstraints {
[super updateViewConstraints];
self.activityIndicatorYConstraint.constant =
[UIScreen mainScreen].bounds.size.height > 480.0f ? 200 : 100;
}
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]
.
当然,您需要输入适当的值而不是200
and 100
。您可能想要定义一些命名常量。另外,不要忘记调用[super updateViewConstraints]
.
回答by Hamza
The problem of @Rob answer's is you should do a lot of code for each constraint.
So to resolve that, just add ConstraintLayout
class to your code and modify constraint constant value for the device that you want in the IB :
@Rob 答案的问题是你应该为每个约束做很多代码。因此,要解决该问题,只需将ConstraintLayout
类添加到您的代码中,并在 IB 中修改您想要的设备的约束常量值:
//
// LayoutConstraint.swift
// MyConstraintLayout
//
// Created by Hamza Ghazouani on 19/05/2016.
// Copyright ? 2016 Hamza Ghazouani. All rights reserved.
//
import UIKit
@IBDesignable
class LayoutConstraint: NSLayoutConstraint {
@IBInspectable
var 3¨5_insh: CGFloat = 0 {
didSet {
if UIScreen.main.bounds.maxY == 480 {
constant = 3¨5_insh
}
}
}
@IBInspectable
var 4¨0_insh: CGFloat = 0 {
didSet {
if UIScreen.main.bounds.maxY == 568 {
constant = 4¨0_insh
}
}
}
@IBInspectable
var 4¨7_insh: CGFloat = 0 {
didSet {
if UIScreen.main.bounds.maxY == 667 {
constant = 4¨7_insh
}
}
}
@IBInspectable
var 5¨5_insh: CGFloat = 0 {
didSet {
if UIScreen.main.bounds.maxY == 736 {
constant = 5¨5_insh
}
}
}
}
Don't forgot to inherit your class constraint from ConstraintLayout
I will add the objective-c version soon
不要忘记继承你的类约束,ConstraintLayout
我很快就会添加objective-c版本
回答by nzs
The basic tool in Auto Layout to manage UI objects' position is the Constraints. A constraint describes a geometric relationship between two views. For example, you might have a constraint that says: “The right edge of progress bar is connected to the left edge of a lable 40 points of empty space between them.”
自动布局中管理 UI 对象位置的基本工具是约束。约束描述了两个视图之间的几何关系。例如,您可能有这样的约束:“进度条的右边缘连接到标签的左边缘,它们之间有 40 点的空白空间。”
This means using AutoLayout you can't do conditional position setting based on UIDevice's mode, rather you can create a view layout which modifies itself if eg. the app runs on 3.5' full screen (IPhone4) or 4' full screen (IPhone5) based on the constraints.
这意味着使用 AutoLayout 你不能根据 UIDevice 的模式进行条件位置设置,而是你可以创建一个视图布局来修改自己,例如。该应用程序根据限制在 3.5' 全屏 (IPhone4) 或 4' 全屏 (IPhone5) 上运行。
So options for your problem using Constraints:
因此,使用约束解决您的问题的选项:
1) find a view on your layout which can be used to create a constraint to position the progressbar relatively. (select the view and the progressbar using CMD button, then use Editor/Pin/Vertical Spacing menu item to create a vertical constraint between the 2 objects)
1)在您的布局上找到一个视图,该视图可用于创建约束来相对定位进度条。(使用 CMD 按钮选择视图和进度条,然后使用 Editor/Pin/Vertical Spacing 菜单项在 2 个对象之间创建垂直约束)
2) create an absolute constraint to stick the progressbar's position to screen edge (keeping space) or centrally
2)创建一个绝对约束将进度条的位置粘贴到屏幕边缘(保持空间)或居中
I found helpful this tutorial about AutoLayout which might be beneficial for you also: http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2
我发现这个关于 AutoLayout 的教程很有帮助,这可能对你也有好处:http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2
Pls note: autolayout only works from IOS 6.
请注意:自动布局仅适用于 IOS 6。
回答by Jure
I generally always try to stay in Interface Builder for setting up constraints. Diving in code to have more control is usually useful if you have completely different layouts on iPhone 4 and 6 for example.
我通常总是尝试留在 Interface Builder 中设置约束。例如,如果您在 iPhone 4 和 6 上有完全不同的布局,则深入代码以获得更多控制通常很有用。
As mentioned before, you can't have conditionals in Interface Builder, that's when linking a constraint to your view controller really comes handy.
如前所述,您不能在 Interface Builder 中使用条件,这时将约束链接到您的视图控制器真的很方便。
Here's a short explanation on 3 approaches to solve Auto Layout issues for different screen sizes: http://candycode.io/how-to-set-up-different-auto-layout-constraints-for-different-screen-sizes/
以下是针对不同屏幕尺寸解决自动布局问题的 3 种方法的简短说明:http: //candycode.io/how-to-set-up-different-auto-layout-constraints-for-different-screen-sizes/
回答by tryKuldeepTanwar
The new way, Without writing a single line!
新的方式,不用写一行!
No need to write device based conditions like these :-
无需编写这样的基于设备的条件:-
if device == iPhoneSE {
constant = 44
} else if device == iPhone6 {
constant = 52
}
I created a library Layout Helperso now you can update constraint for each device without writing a single line of code.
我创建了一个库Layout Helper,因此现在您无需编写一行代码即可更新每个设备的约束。