ios - 自定义 tableView 单元格中的 UILabel 位置

标签 ios swift uitableview

我试图在不使用 Storyboard的情况下制作应用程序,但我遇到了问题。 我设置了自定义单元格,在 iPhone 5s 上一切正常: enter image description here

但是我右边的标签在 iPhone 6s+ 上看起来不对: enter image description here

这是我的代码:

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.

我知道常量设置有问题,但不知道是什么。抱歉,如果这个问题很愚蠢,但这是我的第一个没有 Storyboard的应用。

最佳答案

一种选择是在 addSubview(rightLabel) 之前添加这一行:

rightLabel.autoresizingMask = .FlexibleLeftMargin

关于ios - 自定义 tableView 单元格中的 UILabel 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37054707/

相关文章:

ios - Android Wrap 内容等同于 iOS 中的 Storyboard?

ios - 有没有办法查询设备上有哪些其他应用程序

json - Swift - 解码数组 JSON 数据的数组

ios - UITableView 的 backgroundView 上的触摸事件

swift - 如何连接 UITableViewCell 按钮来执行 UITableView 功能?

ios - 访问 token 过期后处理多个未经授权的请求

ios - UICollectionViewController 未从 NIB 有效加载

ios - 使用文档选择器从 iCloud 中选择一个文件夹

ios - 结合:如何取消平面 map 的发布者

swift - 将 UIToolBar 添加到 TableViewController (Swift)