【问题标题】:Xcode is not letting the view to stay horizontally centeredXcode 不允许视图保持水平居中
【发布时间】:2023-04-03 02:24:01
【问题描述】:

好的,我遇到了这个奇怪的问题(因为我以前从未见过这样的错误),我的视图中有一个 Textfield,我正在通过两侧的动画(尾随和前导)扩大它的宽度,但问题是这是它唯一从一侧扩展的吗(尾随或前导)

我的代码:

VIewController 类

@IBOutlet var constraint1: NSLayoutConstraint! // textfield's leading constraint 
@IBOutlet var constraint2: NSLayoutConstraint! // textfield's trailing constraint 

    viewDidload(){........}

    @IBAction func editingBegin(sender: AnyObject) {

    fullnameTextField.layer.cornerRadius = 0
    fullnameTextField.clipsToBounds = false
    self.constraint2.constant = -16 // here i'm expanding the width by setting constant of my Textfield
    self.constraint1.constant = -16
    UIView.animateWithDuration(duration) {
        self.fullnameTextField.layoutIfNeeded()
    }
}

我的 NSlog 错误:

    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. 
(
    "<NSLayoutConstraint:0x155e77fb0 UITextField:0x155d85e40.trailing == UIView:0x155e46800.trailingMargin>",
    "<NSLayoutConstraint:0x1546b02b0 UITextField:0x155d85e40.leading == UIView:0x155e46800.leadingMargin - 16>",
    "<NSLayoutConstraint:0x1546447e0 UITextField:0x155d85e40.centerX == UIView:0x155e46800.centerX>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1546447e0 UITextField:0x155d85e40.centerX == UIView:0x155e46800.centerX>

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.

这是我设置约束的情节提要

奇怪的是,我在另一个 View 控制器上做同样的事情,它在那里工作但不是在这里,我检查了每个约束它们是正确的,并且 NSLog 错误也出现在那个 ViewController 中(上面是工作正常)

有人知道这里有什么问题吗?请让我知道这太令人沮丧了

我的 ViewController 的代码http://pastebin.com/BvfEvzPE

【问题讨论】:

  • 你的前导约束应该只有16,而尾随是-16
  • @DipenPanchasara 嘿伙计,你的回答成功了
  • 谢谢。快乐编码:)

标签: ios swift2 constraints xcode-storyboard ios-autolayout


【解决方案1】:

尝试移除 AlignX 约束;因为您已经有前导和尾随约束,所以不需要 AlignX。

您可以从这里下载示例代码并与您的进行比较;

http://www.filedropper.com/tst

【讨论】:

  • 感谢您的回复,已经完成了,没有任何变化。
【解决方案2】:

您可以尝试将 CentreX 约束的优先级更改为 999

点击Edit选项,可以将优先级改为999

希望它有效!

【讨论】:

    【解决方案3】:

    首先,正如@Shoaib 所说,删除 Center.x 约束。中心 X + 宽度或前​​导 + 尾随就足够了。

    但即便如此,布局引擎已经为您打破了 Center.x 约束,一切都应该按原样工作。但是,从日志消息来看,只有“前导”约束被修改(有一个常量-16)。 “尾随”似乎没有被修改。 确保您的 IBOutlets 已针对这两个约束进行连接。

    更新:实际上,出口应该没问题,因为它是 Swift,否则它会崩溃。我建议逐行浏览您的代码并查看何时出现破坏约束日志消息。

    UPDATE 2(来自 cmets):您确定视图的顺序在约束中是正确的吗?我的意思是,根据第一个视图和第二个视图,您需要将常量设置为 +16 或 -16。尝试使用 +/-,因为它比进入情节提要和切换视图顺序更快。也许您应该为其中一个约束指定 +16。

    【讨论】:

    • 感谢您的回复,我检查了几次连接是否正确,我还尝试通过删除 centerX 约束仍然没有变化
    • @remyboys,是的,抱歉,我想得不够好。连接不可能为零,因为它是 Swift。否则它会崩溃。查看更新。我会根据您的进一步反馈尝试更新我的答案。
    • 请检查我添加的整个代码pastebin.com/BvfEvzPE,我自己检查了但没有发现任何问题
    • @remyboys 两个建议:首先,确保constraint1constraint2 没有链接到同一个约束(前导约束)。其次,尝试在animateWithDuration 块内移动.constant = -16
    • 没有链接到相同的约束我也认为是这样,但事实并非如此,我将它移到那个块内仍然没有变化
    猜你喜欢
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 2013-03-13
    • 1970-01-01
    • 2017-02-26
    • 2017-08-24
    相关资源
    最近更新 更多