【问题标题】:Recover by breaking constraint in Swift通过在 Swift 中打破约束来恢复
【发布时间】:2015-05-02 16:55:44
【问题描述】:

最近我发现当我在 iOS 7 设备上运行我的应用程序时,编译器会返回一个错误。运行 iOS 8 的设备不会发生这种情况。

我能以某种方式理解这是NSLayoutConstraints 的问题。我不知道是什么让它发生。我需要在不禁用自动布局功能的情况下修复它。

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7be4c240 V:[UIView:0x7be31100]-(11)-|   (Names: '|':UITableViewCellContentView:0x7be2f2c0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-05-02 19:41:28.652 LM[27669:607] -[<CALayer: 0x7bfcf010> display]: Ignoring bogus layer size (99999.000000, 99999.000000)
2015-05-02 19:41:28.681 LM[27669:607] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7bf1d4b0 V:|-(14)-[UIView:0x7bf1a2c0]   (Names: '|':UITableViewCellContentView:0x7bf1a220 )>",
    "<NSLayoutConstraint:0x7bf1d510 V:[UIView:0x7bf1a2c0]-(11)-|   (Names: '|':UITableViewCellContentView:0x7bf1a220 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x81082660 h=--& v=--& V:[UITableViewCellContentView:0x7bf1a220(1)]>"
)

更新:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7bac9bf0 V:[UILabel:0x7bac8c00]-(65)-|   (Names: '|':UIView:0x7bac6f60 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-05-03 00:21:03.508 MesProject[27971:607] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7bac9c20 V:|-(54)-[UILabel:0x7bac8c00]   (Names: '|':UIView:0x7bac6f60 )>",
    "<NSLayoutConstraint:0x7bad4010 V:[UIImageView:0x7bac6490]-(56)-|   (Names: '|':UIView:0x7bac6f60 )>",
    "<NSLayoutConstraint:0x7bac88f0 V:[UILabel:0x7bac8c00]-(8)-[UIImageView:0x7bac6490]>",
    "<NSLayoutConstraint:0x7bac6180 V:[UIView:0x7bac6f60]-(0)-|   (Names: '|':UIView:0x7bac4f20 )>",
    "<NSLayoutConstraint:0x7bac61e0 V:|-(0)-[UIView:0x7bac6f60]   (Names: '|':UIView:0x7bac4f20 )>",
    "<NSLayoutConstraint:0x7bac9a70 V:|-(14)-[UIView:0x7bac4f20]   (Names: '|':UITableViewCellContentView:0x7bac9e80 )>",
    "<NSLayoutConstraint:0x7bac9ad0 V:[UIView:0x7bac4f20]-(11)-|   (Names: '|':UITableViewCellContentView:0x7bac9e80 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7ed4a170 h=-&- v=-&- UITableViewCellContentView:0x7bac9e80.height == UITableViewCellScrollView:0x7bacb240.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x7ed4a7f0 h=-&- v=-&- UITableViewCellScrollView:0x7bacb240.height == MesProject.CardCell:0x7bac9cf0.height>",
    "<NSAutoresizingMaskLayoutConstraint:0x7ed40a00 h=-&- v=--& V:[MesProject.CardCell:0x7bac9cf0(1)]>"
)

当我支持具有行高和动态的 IOS7 时,开始出现此错误。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    return 280
}

我有一个名为CardCell的自定义单元格:

class CardCell: UITableViewCell {

    @IBOutlet var mainView: UIView!
    @IBOutlet weak var text1: UILabel!
    @IBOutlet weak var sharefb: UIButton!

}

是否可以使视图和UITextLabel 动态化?

【问题讨论】:

  • 除了给定的答案,我什么都不知道。由于它无法修复它,我将其删除,希望有人能够帮助您!
  • 你已经尽力了!

标签: ios swift storyboard autolayout


【解决方案1】:
  • UITableViewAutomaticDimension 在 iOS 8 中可用;

  • 在 iOS 7 中,您必须处理委托方法 heightForRowAtIndexPath

实现此代码:

    func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        //here fix, in your case depend on your model
        // -> you manage manually not as magic as UITableViewAutomaticDimension
        return 100
    }

请注意,iOS 8 带来了很多变化,因此管理旧版本的 iOS 有点痛苦。

【讨论】:

  • 好吧,假设我在单元格中有 UILabel,我将如何处理以使大小变为动态,我已经用谷歌搜索了这个问题,但它只能使用不同的 Objective-c 来回答比迅速!
  • 请添加更多详细信息(代码,打印屏幕),以便您有更好的答案。现在,我建议创建 custum tableviewCells。然后根据您的数据使用 tableviewcell1 或 tableviewcell2 作为示例。这取决于您的需求。
猜你喜欢
  • 1970-01-01
  • 2012-07-24
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多