• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 设置自定义 UIView 的不同水平对齐方式

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:39:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

创建了一个自定义 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap