我已将自定义图像添加到我的 UITableView header 部分。当屏幕处于纵向模式时,它工作正常。横向模式下出现大差距。谁能帮帮我?
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headerView = UIView(frame: CGRect(x: 1, y: 1, width: tableView.frame.width, height: 40))
var myLabel = UILabel()
myLabel.frame = CGRectMake(0, 0, tableView.frame.width - 70, 40)
print(myLabel.frame)
myLabel.font = UIFont.boldSystemFontOfSize(18)
myLabel.backgroundColor = UIColor.blueColor()
myLabel.text = self.tableView(tableView, titleForHeaderInSection: section)
let button = UIButton(frame: CGRect(x: 230,y: 0,width: 100,height: 40))
button.tag = section
button.backgroundColor = UIColor.clearColor()
headerView.addSubview(button)
headerView.addSubview(myLabel)
headerView.backgroundColor = UIColor.clearColor()
// the button is image - set image
button.setImage(UIImage(named: "icoDraft"), forState: UIControlState.Normal)
let tapOnCardCell: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(HHLabTestExaminationViewController.handleTapOnSectionImage(_:)))
button.addGestureRecognizer(tapOnCardCell)
return headerView
}
现在标题部分的标题是
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if(section == 0)
{
return "Exam"
}
else if(section == 1)
{
return "News"
}
else if(section == 2)
{
return "Movie"
}
else if(section == 3)
{
return "Sport"
}
return ""
}
最佳答案
当您设置此 header View 时,我没有看到任何自动布局代码。然而,这个简单的布局可以在没有自动布局的情况下处理。
headerView.autoResizesSubviews = true
myLabel.autoResizingMask = [.flexibleWidth, .flexibleHeight]
button.autoResizingMask = .flexibleLeftMargin
关于ios - AutoLayout 问题 UITable View 标题部分 : Adding image in the left does not works fine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42328045/