gpt4 book ai didi

ios - 如何在代码中使用自动布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:29:08 25 4
gpt4 key购买 nike

我想制作一个带有自动布局的自定义tableview,那么我该如何使用自动布局呢?

我觉得应该是这样的:-

self.view.addConstraints([
NSLayoutConstraint(item: square, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 64),
NSLayoutConstraint(item: square, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64),
NSLayoutConstraint(item: square, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: square, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0),
])

最佳答案

嗨,我在我的一个项目中做了这件事:

这是VC的代码,你还需要为你的tableview单元格创建一个自定义类

import UIKit

class ViewController: UIViewController {

//MARK:- view life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.configureTableView()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

//Custom method
func configureTableView(){
let tblview = UITableView(frame: view.bounds, style: .plain)
tblview.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")
tblview.delegate = self
tblview.dataSource = self
self.view.addSubview(tblview)
}

}

extension ViewController: UITableViewDataSource,UITableViewDelegate{

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

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = CustomTableViewCell(style: .default, reuseIdentifier: "CustomTableViewCell")

if indexPath.row == 0 {
cell.backgroundColor = .gray
cell.lblTitle.text = "Title"
cell.lblDescription.text = "MessageMe"
return cell
}
else if indexPath.row == 1{
cell.backgroundColor = .green
cell.lblTitle.text = "TitleTitleTitleTitleTitleTitle"
cell.lblDescription.text = "MessageMe"
return cell
}
else{
cell.backgroundColor = .red
cell.lblTitle.text = "Name"
cell.lblDescription.text = "MessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessag"
return cell
}
}

}

这是自定义类的代码

 import UIKit

class CustomTableViewCell: UITableViewCell {

//Cell View Object
let imgUser = UIImageView()
let lblTitle = UILabel()
let lblDescription = UILabel()

//constant
let paddingWithContent:CGFloat = 20.0
let gapBetweenObject:CGFloat = 10.0
let heightWidthImage:CGFloat = 40.0
let heightTitle:CGFloat = 25.0

//Init
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

//cell object property
imgUser.backgroundColor = .blue
imgUser.contentMode = .scaleAspectFill
lblDescription.numberOfLines = 0
lblTitle.adjustsFontSizeToFitWidth = true
lblTitle.minimumScaleFactor = 0.7

//translatesAutoresizingMas
imgUser.translatesAutoresizingMaskIntoConstraints = false
lblTitle.translatesAutoresizingMaskIntoConstraints = false
lblDescription.translatesAutoresizingMaskIntoConstraints = false

//add to cell
contentView.addSubview(imgUser)
contentView.addSubview(lblTitle)
contentView.addSubview(lblDescription)

//add constraint
NSLayoutConstraint.activate([
//img
imgUser.topAnchor.constraint(equalTo: contentView.topAnchor, constant: paddingWithContent),
imgUser.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: paddingWithContent),
imgUser.widthAnchor.constraint(equalToConstant: heightWidthImage),
imgUser.heightAnchor.constraint(equalToConstant: heightWidthImage),

//title lable
lblTitle.topAnchor.constraint(equalTo: contentView.topAnchor, constant: paddingWithContent),
lblTitle.leadingAnchor.constraint(equalTo: imgUser.leadingAnchor, constant: heightWidthImage + gapBetweenObject),
lblTitle.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -paddingWithContent),
lblTitle.heightAnchor.constraint(equalToConstant: heightTitle),

//description label
lblDescription.topAnchor.constraint(equalTo: lblTitle.bottomAnchor, constant: gapBetweenObject),
lblDescription.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -paddingWithContent),
lblDescription.leadingAnchor.constraint(equalTo: imgUser.leadingAnchor, constant: heightWidthImage + gapBetweenObject),
lblDescription.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -paddingWithContent),

])
}

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

}

关于ios - 如何在代码中使用自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51491986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com