OGeek|极客世界-中国程序员成长平台

标题: ios - 设置自定义 UIView 的不同水平对齐方式 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:39
标题: ios - 设置自定义 UIView 的不同水平对齐方式

创建了一个自定义 UIView 'MyLabelView' ,其中包含两个元素,1x UILabel,1x UIView 将被设置为背景颜色。

MyLabelView

查看:
enter image description here
代码:

import UIKit

@IBDesignable
class MyLabelView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var imageView: UIView!
@IBOutlet weak var titleLabel: UILabel!

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    commonInit()
    contentView?.prepareForInterfaceBuilder()
}

private func commonInit() {
    let bundle = Bundle(for: type(of: self))
    bundle.loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)

    addSubview(contentView)
}

}

我在两次使用 MyLabelView:
- MasterViewController(带有动态原型(prototype)的 TableViewController)
- DetailViewController(带有静态单元格和分组样式的 TableViewController)

MasterViewController
正确显示 MyLabelView 作为自定义 UITableViewCell 的一部分

View:
enter image description here
代码(在cellForRowAt中):

let cell: MyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as! MyTableViewCell
cell.myLabelView.titleLabel.text = "Label"
cell.myLabelView.imageView.backgroundColor = UIColor.red

// Dynamic width to fit label texts of various sizes
var frm: CGRect = cell.myLabelView.titleLabel.frame
frm.size.width = cell.myLabelView.titleLabel.intrinsicContentSize.width
cell.myLabelView.titleLabel.frame = frm


DetailViewController
在这里,我想渲染 MyLabelView 增加字体大小(工作)和右对齐,这样无论标签文本有多长,它总是与人字形对齐

View :
enter image description here
代码(在viewDidLoad中):

myLabelView.titleLabel.text = "Label"
myLabelView.titleLabel.font = myLabelView.titleLabel.font.withSize(17)
myLabelView.imageView.backgroundColor = UIColor.red

// Dynamic width to fit label texts of various sizes
var frm: CGRect = myLabelView.titleLabel.frame
frm.size.width = myLabelView.titleLabel.intrinsicContentSize.width
myLabelView.titleLabel.frame = frm

预期结果
查看:
enter image description here
查看:
enter image description here
我尝试使用额外的外部 UIViews 作为容器并使用 AutoLayout,但不知何故它从来没有像我预期的那样工作。非常感谢您的帮助。
Swift 4、iOS11

更新
我试图在 IB 中设置约束,因为我无法访问单元格(使用静态单元格)。

内容 View :
enter image description here

标题:
enter image description here

MyLabelView:
enter image description here



Best Answer-推荐答案


只需在 UITableViewCell 子类中添加一个函数,并在需要设置右侧旁边的 View 时调用它:

func setTrailingAnchorForView() {
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        contentView.trailingAnchor.constraint(equalTo: imageView.leadingAnchor, constant: 0).isActive = true
    }

这样调用它:

cell.setTrailingAnchorForView()

关于ios - 设置自定义 UIView 的不同水平对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55495384/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4