How to resize UITableView in UITableViewController with section header

后端 未结 5 1133
情话喂你
情话喂你 2020-12-29 13:14

I\'ve got a grouped UITableView in a UITableViewController and I want to resize it horizontally.

I tried many different ways but none of them was perfect.

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 13:45

    Bit late to the party but it can always come handy for others searching for the topic...

    I don't think doing that in the viewDid appear is the right way to do it since it will definitely be visible to the user and resize in front of their eyes. Doesn't make for a great user experience imo.

    In swift 4 I use something like

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        var rect = tableView.frame
        if rect.size.height != h && rect.size.width != w { 
            // set x and y to any value you need. you can also have a 
            // condition about the origin (x,y) 
            // if you have a gap on the left or top...
            rect.size.height = h;
            rect.size.width = w;
            tableView.frame = rect
        }
    }
    

    This will make the changes before the tableView is visible and make the user experience so much better and also as mentioned in the accepted answer you will need to set the window color the color of your table view to blend better (white being the default)

        UIApplication.shared.keyWindow?.backgroundColor = .white
    

提交回复
热议问题