ios - AutoLayout 问题 UITable View 标题部分 : Adding image in the left does not works fine

标签 ios swift uitableview

我已将自定义图像添加到我的 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 ""
}

当屏幕处于纵向时,此图像是我的输出: enter image description here

这是当屏幕处于横向时。我怎样才能解决这个景观差距? enter image description here

最佳答案

当您设置此 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/

相关文章:

ios - 多行 UILabel 在自动 UITableViewCell 上无法正确显示

ios - 通过 segue 传递数据

ios - 如何将自定义 Popover View Controller 与 Xamarin.iOS 中的 ParentViewController 中心对齐?

iphone - 如何在 ios 中生成 SHA256 和 CRC32

ios - 如何在测试期间应用程序初始化之前清除 IOS 钥匙串(keychain)

ios - 无法通过 didSelectRowAtIndexPath 传递值

ios - 在 UITabBarController 中隐藏了 UINavigationController

ios - iPad Retina 模型快速识别

ios - 如何为 ios 拍摄 AGSMapView 的快照?

ios - 将数据从第二个 Controller 发送回前一个 Controller ,它显示 Google 自动完成搜索 Places