gpt4 book ai didi

ios - 自动调整不同屏幕尺寸的 View 对象

转载 作者:行者123 更新时间:2023-11-29 01:41:31 26 4
gpt4 key购买 nike

通常我会在我的项目中取出 Storyboard,然后像下面这样在 View 中写入我的 View 对象,如按钮、标签等:

 class welcomeScreen : UIView {

let btn : UIButton!
override func viewLoad(){
self.backgroundColor = UIColor.whiteColor()
//define frame for Button
btn = UIButton(frame : self.screenAutoLayoutCalc(19.32,42.44,64.73, 15.73))
btn.setTitle ("submit",.Normal)
btn.backgroundColor = UIColor.grayColor()
//add to view
self.addSubview(btn)
}

//calculate object x, y, width and height based by bounds size
func screenAutoLayoutCalc(x:CGFloat,y:CGFloat,w:CGFloat,h:CGFloat) {

let screenSize : CGRect = self.bounds
let returnCG : CGRect = CGRect()
returnCG.origin.x = round((screenSize.width * x)/100)
returnCG.origin.y = round((screenSize.height * x)/100)
returnCG.size.width = round((screenSize.width * w)/100)
returnCG.size.height = round((screenSize.height * h)/100)
return returnCG


}
}

我想知道,在 iOS (swift) 编程中是否有另一种方法可以进行响应式设计?

最佳答案

您可以通过多种方式实现应用中视觉元素的自适应性。您将需要了解 Size Classes

People generally want to use their favorite apps on all their devices and in any orientation. In iOS 8 and later, you use size classes and Auto Layout to help you meet this expectation by defining how the layout of screens, view controllers, and views should adapt when the display environment changes.

和 NSLayoutConstraints。

A constraint defines a relationship between two user interface objects that must be satisfied by the constraint-based layout system.

我建议使用 Storyboard 学习大小类和约束的基础知识,然后慢慢地按照自己的方式编写代码,而不是在 Storyboard 中单击和拖动。

我建议您仔细阅读本文tutorial用于介绍。

关于ios - 自动调整不同屏幕尺寸的 View 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com