根据屏幕大小调整 UIViews 的大小,如 Android in iOS
Resize UIViews depending upon the screen size like in Android in iOS
我使用约束设计了下面的视图,它是在情节提要中的 iPhone 6 上完成的,所以它在 iPhone 6 上看起来完全一样。
当我 运行 它在 iPhone 5s 或 SE 上时,所有视图都会失真。 iPhone X 事情变得更糟了。我了解内容拥抱和内容压缩。
为了解决这个问题,我创建了以下函数来根据屏幕大小调整视图大小。
import UIKit
/// The UI was desinged on this screen
let designedOnSize = CGSize(width: 320, height: 568)
/// This is the phone screen.
let currentScreenSize = UIScreen.main.bounds.size
func getVerticleCostraintValueFrom(_ constant: CGFloat) -> CGFloat {
// cSize * (constant / size)
return currentScreenSize.height*constant/designedOnSize.height
}
func reconfigureVerticleConstraints(_ constraints: [NSLayoutConstraint]) {
for constraint in constraints {
constraint.constant = getVerticleCostraintValueFrom(constraint.constant)
}
}
func getHorizontalCostraintValueFrom(_ constant: CGFloat) -> CGFloat {
// cSize * (constant / size)
return currentScreenSize.width*constant/designedOnSize.width
}
func reconfigureHorizontalConstraints(_ constraints: [NSLayoutConstraint]) {
for constraint in constraints {
constraint.constant = getHorizontalCostraintValueFrom(constraint.constant)
}
}
func configureFontSize(_ views: [Any]) {
for view in views {
if let label = view as? UILabel {
label.font = label.font.withSize(getHorizontalCostraintValueFrom(label.font.pointSize))
continue
}
if let textField = view as? UITextField {
textField.font = textField.font?.withSize(getHorizontalCostraintValueFrom(textField.font!.pointSize))
continue
}
if let textView = view as? UITextView {
textView.font = textView.font?.withSize(getHorizontalCostraintValueFrom(textView.font!.pointSize))
continue
}
if let button = view as? UIButton {
button.titleLabel?.font = button.titleLabel?.font.withSize(getHorizontalCostraintValueFrom(button.titleLabel!.font.pointSize))
continue
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureFontSize([emailTextField, mobileNumberTextField, passwordTextField, signUpButton, orSignUpLabel, facebookButton, googleButton, twitterButton])
reconfigureVerticleConstraints([logoTopConstraint, logoHeightConstraint, emailTextFieldHeightConstraint, emailTextFieldTopConstraint ........])
self.view.layoutIfNeeded()
}
上面的方法对吗?如果没有,那么请给我建议或建议我一些好的做法,因为对我来说表现真的很重要。
这会影响视图的布局吗?
在Android中,这是系统自动处理的,这样的事情在iOS?
中可以完成吗?
相反,您可以通过水平并排添加社交图标来更改登录屏幕的 UI
有了这个你就不会面临调整大小的问题了
我使用约束设计了下面的视图,它是在情节提要中的 iPhone 6 上完成的,所以它在 iPhone 6 上看起来完全一样。 当我 运行 它在 iPhone 5s 或 SE 上时,所有视图都会失真。 iPhone X 事情变得更糟了。我了解内容拥抱和内容压缩。
为了解决这个问题,我创建了以下函数来根据屏幕大小调整视图大小。
import UIKit
/// The UI was desinged on this screen
let designedOnSize = CGSize(width: 320, height: 568)
/// This is the phone screen.
let currentScreenSize = UIScreen.main.bounds.size
func getVerticleCostraintValueFrom(_ constant: CGFloat) -> CGFloat {
// cSize * (constant / size)
return currentScreenSize.height*constant/designedOnSize.height
}
func reconfigureVerticleConstraints(_ constraints: [NSLayoutConstraint]) {
for constraint in constraints {
constraint.constant = getVerticleCostraintValueFrom(constraint.constant)
}
}
func getHorizontalCostraintValueFrom(_ constant: CGFloat) -> CGFloat {
// cSize * (constant / size)
return currentScreenSize.width*constant/designedOnSize.width
}
func reconfigureHorizontalConstraints(_ constraints: [NSLayoutConstraint]) {
for constraint in constraints {
constraint.constant = getHorizontalCostraintValueFrom(constraint.constant)
}
}
func configureFontSize(_ views: [Any]) {
for view in views {
if let label = view as? UILabel {
label.font = label.font.withSize(getHorizontalCostraintValueFrom(label.font.pointSize))
continue
}
if let textField = view as? UITextField {
textField.font = textField.font?.withSize(getHorizontalCostraintValueFrom(textField.font!.pointSize))
continue
}
if let textView = view as? UITextView {
textView.font = textView.font?.withSize(getHorizontalCostraintValueFrom(textView.font!.pointSize))
continue
}
if let button = view as? UIButton {
button.titleLabel?.font = button.titleLabel?.font.withSize(getHorizontalCostraintValueFrom(button.titleLabel!.font.pointSize))
continue
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureFontSize([emailTextField, mobileNumberTextField, passwordTextField, signUpButton, orSignUpLabel, facebookButton, googleButton, twitterButton])
reconfigureVerticleConstraints([logoTopConstraint, logoHeightConstraint, emailTextFieldHeightConstraint, emailTextFieldTopConstraint ........])
self.view.layoutIfNeeded()
}
上面的方法对吗?如果没有,那么请给我建议或建议我一些好的做法,因为对我来说表现真的很重要。 这会影响视图的布局吗?
在Android中,这是系统自动处理的,这样的事情在iOS?
中可以完成吗?相反,您可以通过水平并排添加社交图标来更改登录屏幕的 UI
有了这个你就不会面临调整大小的问题了