UISearchBar on UITableView strange offset issue

前端 未结 4 1849
情书的邮戳
情书的邮戳 2021-01-02 06:10

I have a UITableView which has a UISearchBar subview. This is all on the view of a UIViewController along with a few other subviews (l

相关标签:
4条回答
  • 2021-01-02 06:33

    This happens because a UIViewController's property called automaticallyAdjustsScrollViewInsets

    With iOS 7, UIViewControllers have a property called automaticallyAdjustsScrollViewInsets, and it defaults to YES. If you have a scroll view that is either the root view of your view controller (such as with a UITableViewController) or the subview at index 0, then that property will adjust both the contentInset and the scrollIndicatorInsets. This will allow your scroll view to start its content and scroll indicators below the navigation bar (if your view controller is in a navigation controller).

    From Big Nerd Ranch

    If you are using storyboards, you can change it by selecting the view controller and in the attributes inspector deselect Adjust scroll view insets.

    Here is its description from apple documentation:

    Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

    0 讨论(0)
  • 2021-01-02 06:37

    I have same problem before about position of tableview and searchbar. i tried the following and it works for me.

    If you do not write code for that and if it is only problem of xib or storyboard then try all outlet's autosizing and origin setting to fix its position and see the difference. it may be work for you.

    enter image description here

    0 讨论(0)
  • 2021-01-02 06:38

    Update : automaticallyAdjustsScrollViewInsets has been deprecated in ios 11 and a new field contentInsetAdjustmentBehavior has been introduced.

    if #available(iOS 11.0, *) {
        tableview.contentInsetAdjustmentBehavior = .never
    } else {
        automaticallyAdjustsScrollViewInsets = false
    }
    
    0 讨论(0)
  • 2021-01-02 06:45

    UITableView header can contains only one UIView, so if you need UISearchBar plus UILabel, you need to wrap they into UIView and add this view as UITableView header.

    0 讨论(0)
提交回复
热议问题