dsk1306 dsk1306 - 6 months ago 36
Swift Question

UILabel position in custom tableView cell

I'm trying to make an app without using storyboards and I have a problem.
I've set up custom cell and everything looks ok on iPhone 5s:
enter image description here

But my right label looks wrong on, for example, iPhone 6s+:
enter image description here

Here is my code:

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)

}


}


Update:
I've tried autolayout:

leftLabel.leadingAnchor.constraintEqualToAnchor(self.layoutMarginsGuide.leadingAnchor, constant: 20).active = true
leftLabel.centerYAnchor.constraintEqualToAnchor(self.layoutMarginsGuide.centerYAnchor).active = true


and this is result

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.


I know that something wrong with constant setting, but don't know what. Sorry if this question is stupid but this is my first app without storyboards.

Answer

One option is to add this line just before addSubview(rightLabel):

rightLabel.autoresizingMask = .FlexibleLeftMargin
Comments