UITableView goes under translucent Navigation Bar

前端 未结 14 1104
一整个雨季
一整个雨季 2020-12-22 23:51

I am trying to have a transparent navigation bar in IOS 7 app. There is a full screen image in my application. I am also having a UITableView over that image. When I use the

14条回答
  •  一个人的身影
    2020-12-23 00:16

    Better not to hardcode the Inset values as it might based on the orientation of the device.

    Code:

    func setTableViewContentInset() {
    
        let contentInsetHeight = topLayoutGuide.length
        let contentInset = UIEdgeInsetsMake(contentInsetHeight, 0, 0, 0)
    
        tableView.contentInset = contentInset
        tableView.scrollIndicatorInsets = contentInset
    }
    
    func scrollToTop() {
    
        if tableView.indexPathsForVisibleRows?.count > 0 {
    
            let topIndexPath = NSIndexPath(forRow: 0, inSection: 0)
    
            tableView.scrollToRowAtIndexPath(topIndexPath, atScrollPosition: .Top, animated: false)
        }
    }
    
    func scrollToTopOfVisibleCells() {
    
        if let visibleIndexPaths = tableView.indexPathsForVisibleRows where tableView.indexPathsForVisibleRows?.count > 0 {
    
            let topMostVisibleIndexPath = visibleIndexPaths[0]
    
            tableView.scrollToRowAtIndexPath(topMostVisibleIndexPath, atScrollPosition: .Top, animated: false)
    
        }        
    }
    
    //MARK: Load Views
    override func viewDidLoad() {
        super.viewDidLoad()
    
        setTableViewContentInset()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    
        scrollToTop()
    }
    
    //MARK: Trait collection change
    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
    
        setTableViewContentInset()
        scrollToTopOfVisibleCells()
    }
    

提交回复
热议问题