【问题标题】:UIButton not working in landscapeUIButton 不能在横向工作
【发布时间】:2016-06-05 15:20:50
【问题描述】:

在我的 iOS 应用程序的一个 ViewController 中,有一个具有此功能的 UIButton:

@IBAction func openSettings(sender: AnyObject) {
    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}

这可以正常工作,但是当从纵向更改为横向时,按钮不再起作用。我正在为此按钮使用自动布局。控制台这样说:

2016-06-05 17:15:31.238 MyApp[3975:1756888] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<_UILayoutSupportConstraint:0x15cf4a540 V:[_UILayoutGuide:0x15cf685a0(0)]>",
    "<_UILayoutSupportConstraint:0x15cd5c7d0 V:|-(0)-[_UILayoutGuide:0x15cf685a0]   (Names: '|':UIView:0x15e202490 )>",
    "<NSLayoutConstraint:0x15cf68f60 V:[_UILayoutGuide:0x15cf685a0]-(265)-[UIButton:0x15cf65310'Enable Push!']>",
    "<NSLayoutConstraint:0x15cf68fb0 UIButton:0x15cf65310'Enable Push!'.centerY == UIView:0x15e202490.centerY>",
    "<NSLayoutConstraint:0x15cf4b640 'UIView-Encapsulated-Layout-Height' V:[UIView:0x15e202490(414)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15cf68f60 V:[_UILayoutGuide:0x15cf685a0]-(265)-[UIButton:0x15cf65310'Enable Push!']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

有人知道或看到原因吗?

【问题讨论】:

    标签: ios iphone swift uibutton autolayout


    【解决方案1】:

    您有两个相互冲突的约束。第一个

    V:[_UILayoutGuide:0x15cf685a0]-(265)-[UIButton:0x15cf65310'Enable Push!']
    

    正在尝试将按钮放置在距顶部布局指南 265 点的位置。第二个:

    UIButton:0x15cf65310'Enable Push!'.centerY == UIView:0x15e202490.centerY
    

    正在尝试使按钮在视图中垂直居中。决定是否希望按钮位于距顶部 265 点或垂直居中并删除其他约束。

    【讨论】:

    • 做到了,现在可以工作了。我删除了“从顶部开始的 265 点”。但无论如何这很奇怪,因为我使用了“添加缺失的约束”。这一定是 Xcode 中的一个错误,对吧?现在我收到一个警告:框架“按钮”在运行时会有所不同。但至少它有效。
    • “添加缺失的约束”最好避免。它需要猜测你想要什么,但倾向于绝对定位。最好自己设置你想要的约束。您可以通过在 IB 窗口底部为项目选择“更新框架”来消除框架警告
    【解决方案2】:

    您收到此警告的原因是您没有正确设置此按钮的自动布局。您添加了一个不需要的额外约束。您可以删除顶部约束 (265),因为您已经设置了按钮的高度并对齐到视图的垂直中心。

    【讨论】:

    • 做到了,现在可以工作了。我删除了“从顶部开始的 265 点”。但无论如何这很奇怪,因为我使用了“添加缺失的约束”。这一定是 Xcode 中的一个错误,对吧?现在我收到一个警告:框架“按钮”在运行时会有所不同。但至少它有效。
    • 不,这不是 Xcode 中的错误。您能分享一下您为此按钮设置的限制条件吗?
    • 只需添加按钮的高度限制。
    • 哦,当然,你是对的。做到了。它现在正在工作,没有任何警告。为什么 Xcode 不这样做?为什么我必须手动操作?
    • 当您单击“添加缺少的约束”错误时。 Xcode 会自动添加它。另一种方法是通过“重置为建议的约束”来完成。但它不是首选,因为它不会根据您的需要设置约束。它只会设置约束,这样您就不会看到任何错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多