ios - UITableView 圆角半径无法正常工作

标签 ios objective-c swift xcode

我有一个表格 View ,我想让单元格变圆。每个单元格内部都有一个文本字段。
在“cellForRowAtIndexPath”上,我写道:

cell.layer.cornerRadius = cell.frame.size.height/2;
cell.layer.masksToBounds = true;
cell.clipsToBounds = true;
但我不明白为什么,但在一行单元格中,有时角半径不起作用导致像“尖”。
附:内容模式设置为 Aspect fit
有人能帮我吗?

最佳答案

我建议您向单元 Controller 添加一个容器 View ,您将能够使用自动布局更好地管理单元填充:

let containerView = UIView() 
设置它的属性:
containerView.backgroundColor = .red
containerView.clipsToBounds = true
containerView.translatesAutoresizingMaskIntoConstraints = false
现在添加 UIView 并设置约束:
addSubview(containerView)
containerView.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true //change the constant to add padding (right and bottom are negative value)
containerView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0).isActive = true
containerView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0).isActive = true
containerView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true 
之后在 cellForRowAt 中设置你的单元格背景颜色以清除
cell.backgoundColor = .clear
cell.containerView.layer.cornerRadius = cell.bounds.size.height/2;
这是将角半径应用于 containerView 的结果:
enter image description here
或者像这样设置你的 tableView 和单元格:
class DummyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

let tableView = UITableView()

override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.delegate = self
    tableView.dataSource = self
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.register(MyCell.self, forCellReuseIdentifier: "cellId")

    view.addSubview(tableView)
    tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    10
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    300
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! MyCell
    cell.layer.cornerRadius = cell.frame.size.height / 2;
    return cell
  }
}
你的手机:
class MyCell: UITableViewCell {

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    backgroundColor = .blue
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
 }
}
这是应用圆角半径的结果:
enter image description here

关于ios - UITableView 圆角半径无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63864281/

相关文章:

ios - 以编程方式点击选择 UITabBar View

objective-c - 从 Cocoa 中的 NSTableView 中删除选定的行

objective-c - UITabBar自定义

ios - 不知道部分中的行数(快速)

SWift SKShapeNode 多线粗细和颜色

iOS 5 - AVAPlayer 不再工作

ios - CAGradientLayer 不能改变颜色

ios - 从点击手势识别器获取标签

ios - UIImageView 中的大小限制?

iphone - Facebook IOS SDK 可选权限