如何使UITableView忽略UINavigationController

时间:2017-07-05 10:16:59

标签: ios swift xcode uitableview

所以我有这个屏幕:

Current TableViewController

此图像是UIView的子视图,我将其用作表格的标题:

class CoursesDetailPageHeader: UITableViewHeaderFooterView {

    @IBOutlet weak var imageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()

        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
    }

    func setDefaultImage() {
        imageView.image = UIImage(named: "header")
    }
}

我的tableView:

override func viewDidLoad() {
    super.viewDidLoad()

    let headerNib = UINib(nibName: "CoursesDetailPageHeader", bundle: nil)
    tableView.register(headerNib, forHeaderFooterViewReuseIdentifier: "pageHeader")
}

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

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let pageHeader = tableView.dequeueReusableHeaderFooterView(withIdentifier: "pageHeader") as! CoursesDetailPageHeader
    pageHeader.setDefaultImage()

    return pageHeader
}

我应该怎么做才能将tableView带到顶部,这样就没有空的空间(透明的navController)?

UPD: 显示我想要的东西: Desired TableViewController

解决: 感谢@kathayatnk问题解决了以下代码:

let statusBarHeight = UIApplication.shared.statusBarFrame.height
let navigationBarHeight = navigationController!.navigationBar.frame.size.height
let insets = UIEdgeInsets(top: -(statusBarHeight + navigationBarHeight), left: 0, bottom: 0, right: 0)
self.tableView.contentInset = insets

2 个答案:

答案 0 :(得分:1)

为此,您必须UINavigationBar Translucent

<强> AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    UINavigationBar.appearance().isTranslucent = true
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

    return true
}

答案 1 :(得分:1)

您可以设置嵌入式tableView的插图

let insets = UIEdgeInsets(top: -64.0, left: 0, bottom: 0, right: 0)
self.tableView.contentInset = insets;