Weird uitableview behaviour in iOS11. Cells scroll up with navigation push animation

前端 未结 12 1454
粉色の甜心
粉色の甜心 2021-01-29 18:34

I have recently migrated some code to new iOS 11 beta 5 SDK.

I now get a very confusing behaviour from UITableView. The tableview itself is not that fancy. I have custo

12条回答
  •  悲哀的现实
    2021-01-29 19:10

    This seems more like a bug than intended behavior. It happens when navigation bar is not translucent or when background image is set.

    If you just set contentInsetAdjustmentBehavior to .never, content insets won't be set correctly on iPhone X, e.g. content would go into bottom area, under the scrollbars.

    It is necessary to do two things:
    1. prevent scrollView animating up on push/pop
    2. retain .automatic behaviour because it is needed for iPhone X. Without this e.g. in portrait, content will go below bottom scrollbar.

    New simple solution: in XIB: Just add new UIView on top of your main view with top, leading and trailing to superview and height set to 0. You don't have to connect it to other subviews or anything.

    Old solution:

    Note: If you are using UIScrollView in landscape mode, it still doesn't set horizontal insets correctly(another bug?), so you must pin scrollView's leading/trailing to safeAreaInsets in IB.

    Note 2: Solution below also has problem that if tableView is scrolled to the bottom, and you push controller and pop back, it will not be at the bottom anymore.

    override func viewDidLoad()
    {
        super.viewDidLoad()
    
        // This parts gets rid of animation when pushing
        if #available(iOS 11, *)
        {
            self.tableView.contentInsetAdjustmentBehavior = .never
        }
    }
    
    override func viewDidDisappear(_ animated: Bool)
    {
        super.viewDidDisappear(animated)
        // This parts gets rid of animation when popping
        if #available(iOS 11, *)
        {
            self.tableView.contentInsetAdjustmentBehavior = .never
        }
    }
    
    override func viewDidAppear(_ animated: Bool)
    {
        super.viewDidAppear(animated)
        // This parts sets correct behaviour(insets are correct on iPhone X)
        if #available(iOS 11, *)
        {
            self.tableView.contentInsetAdjustmentBehavior = .automatic
        }
    }
    

提交回复
热议问题