转换后addSubview无法正常工作

时间:2017-01-01 20:54:38

标签: ios uiview swift2 xcode8 addsubview

我将xcode从7.3.1升级到8并转换为swift 2.2,除了故事板是乱七八糟的东西,除了以编程方式添加UIView之外。

此前工作正常,现在根本没有任何显示,也没有错误。有什么想法吗?

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //create view
    let vw = UIView()
    vw.backgroundColor = UIColor(red: 0.9137, green: 0.9176, blue: 0.9216, alpha: 1.0) /* #e9eaeb */

    //create label inside view
    let label: UILabel = UILabel()
    label.frame =  CGRectMake(0, 0, 200, 28)
    label.textAlignment = NSTextAlignment.Left
    label.textColor = UIColor.grayColor()
    label.font = label.font.fontWithSize(13)

    if !clearSearch {
        label.text = "  Recent Searches"
    } else {
        label.text = "  Search Results"
    }
    vw.addSubview(label)

    //create button inside view
    let button: UIButton = UIButton()
    button.frame = CGRectMake(self.view.frame.size.width - 45, 0, 40, 28)
    button.titleLabel?.font = UIFont.systemFontOfSize(13)
    button.setTitle("Clear", forState: UIControlState.Normal)
    button.addTarget(self, action: #selector(deleteSearches), forControlEvents: UIControlEvents.TouchUpInside)
    button.setTitleColor(UIColor(red: 0.3216, green: 0.7176, blue: 0.5333, alpha: 1.0), forState: UIControlState.Normal)
    vw.addSubview(button)

    return vw
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

现在看来这是必需的。

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 28
}