ios - 根据屏幕大小调整 UIViews 的大小,就像在 iOS 中的 Android 中一样

标签 ios ios-autolayout autoresize

我使用约束设计了下面的 View ,它是在 iPhone 6 的 Storyboard 中完成的,所以它在 iPhone 6 上看起来完全一样。 当我在 iPhone 5s 或 SE 上运行它时,所有 View 都会失真。在 iPhone X 上,情况变得更糟。我了解内容拥抱和内容压缩。

enter image description here

为了克服这个问题,我创建了以下函数来根据屏幕大小调整 View 大小。

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()
}

上面的方法对吗?如果没有,那么请给我建议或建议我一些好的做法,因为对我来说表现真的很重要。 这会影响 View 的布局吗?

在 Android 中,这是由系统自动处理的,这些事情在 iOS 中可以完成吗?

enter image description here

最佳答案

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

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

引用:https://dribbble.com/shots/3803685-Fintech-App-UI

关于ios - 根据屏幕大小调整 UIViews 的大小,就像在 iOS 中的 Android 中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49333178/

相关文章:

ios - IOS 上的 Qt QML 应用程序运行缓慢,JIT 被禁用

iphone - 带有 webview 崩溃的页面 View Controller

ios - 自动布局 UITextfield

ios - 为什么我会收到一条错误消息,指出自动布局为 'Unable to simultaneously satisfy constraints.'?

自动调整大小和滚动的 Java JTextArea

iphone - 画廊上的自动旋转 - willAnimateRotation... 未调用

objective-c - IOS 6 奇怪的调整大小

ios - 使用AVFoundation框架进行视频录制

ios - 内购收据校验, "in_app"字段为空

swift - 如何快速修复导航栏下的 View