我试图在不使用故事板的情况下制作应用,但我遇到了问题。 我已经设置了自定义单元格,在iPhone 5s上一切正常:
这是我的代码:
class CustomTableViewCell: UITableViewCell {
var leftLabel = UILabel()
var currencyValueLabel = UILabel()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
leftLabel = UILabel(frame: CGRect(
x: 16,
y: 8,
width: 100,
height: self.bounds.size.height-16
))
leftLabel.textAlignment = .Left
addSubview(leftLabel)
currencyValueLabel = UILabel(frame: CGRect(
x: self.bounds.size.width-116,
y: 8,
width: 100,
height: self.bounds.size.height-16
))
rightLabel.textAlignment = .Right
addSubview(rightLabel)
}
}
更新 我尝试过自动布局:
leftLabel.leadingAnchor.constraintEqualToAnchor(self.layoutMarginsGuide.leadingAnchor, constant: 20).active = true
leftLabel.centerYAnchor.constraintEqualToAnchor(self.layoutMarginsGuide.centerYAnchor).active = true
这是结果
2016-05-05 18:49:09.849 RubApp[57636:2228350] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x7fc503d21c50 h=--& v=--& UILabel:0x7fc503cea100'EUR'.midX == + 66>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc503d1ea40 h=--& v=--& H:[UILabel:0x7fc503cea100'EUR'(100)]>",
"<NSLayoutConstraint:0x7fc503ce7c00 UILabel:0x7fc503cea100'EUR'.leading == UILayoutGuide:0x7fc503ce8c20'UIViewLayoutMarginsGuide'.leading + 20>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc503ce98e0 h=-&- v=--& 'UIView-Encapsulated-Layout-Left' H:|-(0)-[RubApp.MainViewCell:0x7fc505868000'idTableCellMainView'] (Names: '|':UITableViewWrapperView:0x7fc505869600 )>",
"<NSLayoutConstraint:0x7fc503ce7df0 'UIView-leftMargin-guide-constraint' H:|-(15)-[UILayoutGuide:0x7fc503ce8c20'UIViewLayoutMarginsGuide'](LTR) (Names: '|':RubApp.MainViewCell:0x7fc505868000'idTableCellMainView' )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc503ce7c00 UILabel:0x7fc503cea100'EUR'.leading == UILayoutGuide:0x7fc503ce8c20'UIViewLayoutMarginsGuide'.leading + 20>
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.
2016-05-05 18:49:09.857 RubApp[57636:2228350] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x7fc503e5a720 h=--& v=--& UILabel:0x7fc503e49b50'USD'.midX == + 66>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc503e5a940 h=--& v=--& H:[UILabel:0x7fc503e49b50'USD'(100)]>",
"<NSLayoutConstraint:0x7fc503e56f50 UILabel:0x7fc503e49b50'USD'.leading == UILayoutGuide:0x7fc503e4a3a0'UIViewLayoutMarginsGuide'.leading + 20>",
"<NSAutoresizingMaskLayoutConstraint:0x7fc503e5ab70 h=-&- v=--& 'UIView-Encapsulated-Layout-Left' H:|-(0)-[RubApp.MainViewCell:0x7fc504030a00'idTableCellMainView'] (Names: '|':UITableViewWrapperView:0x7fc505869600 )>",
"<NSLayoutConstraint:0x7fc503e4a780 'UIView-leftMargin-guide-constraint' H:|-(15)-[UILayoutGuide:0x7fc503e4a3a0'UIViewLayoutMarginsGuide'](LTR) (Names: '|':RubApp.MainViewCell:0x7fc504030a00'idTableCellMainView' )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc503e56f50 UILabel:0x7fc503e49b50'USD'.leading == UILayoutGuide:0x7fc503e4a3a0'UIViewLayoutMarginsGuide'.leading + 20>
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.
我知道不断设置有问题,但不知道是什么。很抱歉,如果这个问题很愚蠢,但这是我的第一个没有故事板的应用程序。
答案 0 :(得分:3)
一种选择是在addSubview(rightLabel)
之前添加此行:
rightLabel.autoresizingMask = .FlexibleLeftMargin
答案 1 :(得分:0)
您需要添加NSLayoutConstraint
告诉右侧的标签,使其具有x的尾随约束。