Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ios中设置约束的好方法或好做法是什么,在ipad设备中设计得很好_Ios_Swift_Autolayout_Constraints - Fatal编程技术网

在ios中设置约束的好方法或好做法是什么,在ipad设备中设计得很好

在ios中设置约束的好方法或好做法是什么,在ipad设备中设计得很好,ios,swift,autolayout,constraints,Ios,Swift,Autolayout,Constraints,我已经创建了一个在iphone设备上运行良好的应用程序,我尝试了自动布局和约束,这样它在ipad上的iphone设备上也可以运行,但问题是在iphone上看是可以的,但在ipad设备上距离似乎太远。例如,如果我将约束距离设置为8,在iphone设备中可以,但在ipad设备中似乎很宽。还有没有一种方法可以使图像或按钮或组件的大小更大,以适应ipad?是否有一个好的做法或最好的方式来设置设计,这也适用于ipad。非常感谢。正如你在下面的图片中看到的,这些数字和拥有的距离是它在iPhone上的外观。截

我已经创建了一个在iphone设备上运行良好的应用程序,我尝试了自动布局和约束,这样它在ipad上的iphone设备上也可以运行,但问题是在iphone上看是可以的,但在ipad设备上距离似乎太远。例如,如果我将约束距离设置为8,在iphone设备中可以,但在ipad设备中似乎很宽。还有没有一种方法可以使图像或按钮或组件的大小更大,以适应ipad?是否有一个好的做法或最好的方式来设置设计,这也适用于ipad。非常感谢。正如你在下面的图片中看到的,这些数字和拥有的距离是它在iPhone上的外观。截图是在ipad上拍摄的。这就是它在ipad上的外观。我设法将限制设置为按钮开始、跳过和asnwer,但我想调整所有ipad的大小,包括问题和其他带有数字的按钮,如图中所示,我想使其变大并与中心相等。

假设在故事板中,屏幕底部固定有一个跳过按钮(您没有更改底部销,因此我没有将其包括在内),它的高度为44点,距离屏幕左侧8点,距离屏幕右侧8点。这对于iPhone来说很好,但在iPad上,你希望按钮的高度为50点,距离屏幕左侧100点,距离屏幕右侧100点

在视图控制器内部,您需要为类型为
NSLayoutConstraint
的约束创建一个出口,并且该控制器上有一个
常量
属性,您可以使用该属性更改在情节提要内设置的常量(请参见下面的代码示例)

如果你不知道如何建立约束连接,你所做的就是在故事板中选择约束,打开助理编辑器,然后cntrl+将约束拖动到视图控制器,并命名为任何你想命名的名称。这与连接按钮、标签或图像视图的过程完全相同,唯一的区别是您可以使用约束

每个应用程序都有一个属性,您可以在其中找到当前正在使用的设备类型:
UIDevice.current.userInterfaceIdiom

在viewDidLoad中,您可以了解用户使用的设备类型,然后将限制条件更改为适合您的iPad大小:

// skipButton is type UIButton (look at what's next to the name skipButton). You are not changing the button you are changing the constraint on the button.
@IBOutlet weak fileprivate var skipButton: UIButton! // the main focus here are the constraints for the saveButton. I just added this here to show that the saveButton is inside this vc. Your focus is changing the below constraints that help position and size the saveButton and not the saveButton itself

// ***VERY IMPORTANT*** YOU HAVE TO CONNECT THESE CONSTRAINTS via STORYBOARD (look at the 3 pics below). You do it the same way you would have connected the skipButton above. The difference is you grab the constraint that is on skipButton that you made in storyboard. The skipButton is already connected. These are the constraints that are on it and are completely different from the skipButton itself.
// the constraints are of type NSLayoutConstraint (look at the name next to skipButtonLeadingConstraint). This is what you want to change. This and the UIButton above are two completely different things
@IBOutlet weak var skipButtonLeadingConstraint: NSLayoutConstraint! // in Storyboard left constraint is set at 8 points from left side of vc
@IBOutlet weak var skipButtonTrailingConstraint: NSLayoutConstraint! // in Storyboard right constraint is set at 8 points from right side of vc
@IBOutlet weak var skipButtonHeightConstraint: NSLayoutConstraint! // in Storyboard height is set at 44 points

override func viewDidLoad() {
    super.viewDidLoad()

    // if the user is using an iPad then change the constraints to the below sizes which would make the saveButton's width smaller and also it's height taller. If the user is using anything other then an iPad then it will automatically use the Storyboard constraints and automatically ignore the below code
    if UIDevice.current.userInterfaceIdiom == .pad{

        // when using an iPad left constraint is now set at 100 points from left side of vc
        skipButtonLeadingConstraint.constant = 100

        // when using an iPad right constraint is now set at 100 points from right side of vc
        skipButtonTrailingConstraint.constant = 100

        // when using an iPad height constraint is now set at 50 points high
        skipButtonHeightConstraint.constant = 50
    }
}
下面是如何在故事板中添加SkipButtonReadingConstraint。您可以在Skip按钮的约束下(以蓝色突出显示)在文档大纲中获取它

首先,选择文档大纲中的约束:

第二步将连接拖动到视图控制器,并将其命名为SkipButtonReadingConstraint:

第三,完成连接:

您可以使用AspectRatio使用故事板设置约束

静态距离值8对iPhone来说是可以的,但在iPad中要小得多,所以要解决这个问题,需要根据屏幕大小提供距离

步骤

1) 在
ViewController
中拖动视图,并将其水平和垂直设置为在容器中输入的

2) 设置与屏幕大小相同的宽度和高度(ViewController)

3) 现在选择“高度”参数并检查乘数是否设置为1

4) 根据需要改变乘数值

现在,输出将与iPhone和iPad相同,外观相同

间距

1) 拖动标签以设置间距,并将其水平添加到中心位置,同时添加之前添加的视图

2) 设置顶部空间以查看添加的标签,如下所示

3) 现在选择添加的顶部约束,并再次改变其乘数,以根据viewScreen大小设置它们之间的间距

选择“顶部约束”类似于“乘数1”

改变乘数后,标签向下移动

输出


为了简化事情,苹果公司推荐了一种新的模式。与其根据多种设备类型、分辨率、多任务模式和设备方向来考虑布局,不如将布局调整为两种宽度(称为紧凑型和规则型)和两种高度(同样紧凑型和规则型)。这些区别被称为大小等级。然后可以使用这些尺寸类定义或调整布局


请。

这些限制是程序性的还是故事板上的?请看上面的故事板Sir。我想让这些按钮带有数字,包括大到可以放在ipad上的数字,并根据iPhone的外观定位到中间。你必须抓住所有需要更改的限制,然后如果UIDevice.current.userInterfaceIdiom==.pad{},则在内部更改它。你也可以在故事板上做,但我不知道怎么做。我能给你提供项目文件吗,先生?我真的需要设置约束的帮助只是为了设置约束,在ipad中可以很好地工作。你还在吗,先生?好的,先生,我今天或明天等你