【问题标题】:UIButton and UILabel not clickable on smaller screensUIButton 和 UILabel 在较小的屏幕上不可点击
【发布时间】:2020-01-20 14:04:46
【问题描述】:

我创建了一个自定义下拉列表,它是 UIButtons 的堆栈视图。并制作了 UIlabel 文本(服务条款)的可点击部分。 两者都在 iphone XR 上完美运行(可以点击它们)。

该问题仅出现在较小的设备上(iphone 6,7)。下拉列表中的最后一个 UIButton 可在按钮标题上方点击,而不是在标签或整个按钮空间上。 UILabel(服务条款)是完全不可点击的。 Iphone XR 运行良好。

Not clickable UIButton

Not clickable UILabel

监听 UILabel tap(在较小的设备上不调用):

 @objc func tapLabel(gesture: UITapGestureRecognizer) {
    print("TapLabel")
    let text = "By upgrading your account, you agree to our Terms of Service."
    let termsRange = (text as NSString).range(of: "Terms of Service.")

    if gesture.didTapAttributedTextInLabel(label: textTerms, inRange: termsRange) {
        if let url = URL(string: "https://xxxxx/terms") {
            UIApplication.shared.open(url)
        }
    } else {
        print("Tapped none")
    }
}

界面构建器: Interface overview

布局约束日志,当我打开一个下拉列表时:

"<NSLayoutConstraint:0x2823f4230 UIButton:0x109505a70'Quarterly Subscription - ...'.height == 50   (active)>",
"<NSLayoutConstraint:0x2823f5680 V:[UIButton:0x10950d760'Quarterly Subscription - ...']-(0)-[UIButton:0x109505a70'Quarterly Subscription - ...']   (active)>",
"<NSLayoutConstraint:0x2823f5a40 V:[UIButton:0x109505a70'Quarterly Subscription - ...']-(0)-[UIButton:0x10950d260'Monthly Subscription - 13...']   (active)>",
"<NSLayoutConstraint:0x2823f48c0 'UISV-spacing' V:[UIButton:0x10950d760'Quarterly Subscription - ...']-(0)-[UIButton:0x10950d260'Monthly Subscription - 13...']   (active)>"
)

    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x2823f4230 UIButton:0x109505a70'Quarterly Subscription - ...'.height == 50   (active)>

任何想法,这种行为的原因可能是什么?非常感谢。

【问题讨论】:

    标签: ios swift xcode interface-builder


    【解决方案1】:

    您可能已经设置了相对于超级视图的标签/按钮的宽度和高度,因此它们的尺寸非常小或其他视图位于它们之上

    1. 给标签和按钮一些背景颜色,然后在小屏幕上检查,你就会知道问题所在

    2. 在调试模式下查看他们的视图层次结构

    【讨论】:

    • 谢谢,问题出在容器的高度太小或未设置。通过使用层次结构调试,问题更加明显,
    猜你喜欢
    • 2017-10-12
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多