【问题标题】:Resize UIViews depending upon the screen size like in Android in iOS根据屏幕大小调整 UIViews 的大小,如 iOS 中的 Android
【发布时间】:2018-08-26 06:06:56
【问题描述】:

我使用约束设计了下面的视图,它是在 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中可以做到吗?

【问题讨论】:

  • 使用屏幕大小来直接计算布局并不是一个好主意。当引入更多屏幕尺寸时,这可能是一个问题。您应该使用通过乘数或纵横比约束设置对象大小和位置的布局约束。例如,您可以将按钮的高度限制为 1:5 到根视图的高度。
  • Storyboard 约束用于使您的设计在不同的屏幕尺寸上工作,这是它们的主要目的,因此更简单的设计不需要编码。您能否举例说明不同设备上到底出了什么问题?
  • @Paulw11 这是一个非常好的主意,我如何计算按钮和视图之间的比率,因为一旦我使按钮宽度相等以查看它将占用视图宽度,我不会得到确切的大小。你有什么诀窍吗?
  • @Paulw11 我们通常是否也会根据手机大小更改字体大小,而不是 iPad,仅取决于手机。你看我也为此创建了一个函数。我认为有必要将 textField 的高度设置为 rootView 的 1:10,然后在 iPhone 5 上,UITextField 的所有高度都会减小,我认为它不会减小内部文本的大小。
  • 您可以为文本字段设置字体缩放。您还应该支持动态类型,以便用户偏好也可以调整您的字体大小

标签: ios ios-autolayout autoresize


【解决方案1】:

相反,您可以通过水平相邻添加社交图标来更改登录屏幕的 UI

这样你就不会面临调整大小的问题

参考:https://dribbble.com/shots/3803685-Fintech-App-UI

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多