Tag: 程序化自动布局

iOS(Swift):具有可变高度的TableView节标题-适合内容

由于要求,必须根据“设置”更改动态更改TableView的Header部分的内容。 问题 :随着节标题内容的动态变化,节标题的高度必须改变以适应变化的内容。 解决方案 : 以下是实现解决方案的步骤 步骤1 : 设置TableView以支持自动调整大小 覆盖func viewDidLoad(){ super.viewDidLoad() self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension; self.tableView.estimatedSectionHeaderHeight = 38 } 步骤2 : 区段标题 使用自动布局创建节标题视图,这里我们使用NSLayoutConstraint动态创建自动布局UIView func tableView(_ tableView:UITableView,viewForHeaderInSection部分:Int)-> UIView? { 让headerView = UIView() 变量约束= [NSLayoutConstraint]() 如果section == 0 { 让flightStatsNoticeLabel = UILabel() flightStatsNoticeLabel.text =“ Lorem Ipsum只是印刷和排版行业的虚拟文本。 自1500年代以来,Lorem Ipsum一直是业界标准的伪文本,当时一位不知名的打印机拿起一个厨房,然后将其打乱成一本样本书。” flightStatsNoticeLabel.numberOfLines = 0 flightStatsNoticeLabel.sizeToFit() 让goToSettingsButton = UIButton() goToSettingsButton.setTitle(“ GoToSettings”,用于:.normal) […]