Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout issues in UITableView after updating to Xcode 9

After I updated to Xcode 9, UITableView in iOS 10 device is behaving very strangely. The tableView cell sizes are not maintained. It it resizing by itself and not following the constraints and cell sizes I had explicitly coded. The gap between the tableView cells is black all of a sudden. However, these layout issues are not reciprocated in iOS 11 handset. In a nutshell, the layout is completely screwed in iOS 10 devices. Is anyone else having backward compatibility after updating to Xcode 9? If so how did you resolve it.

To change the frame width of the custom table View cell cell I am using the frame property. The code I am using is

    override var frame: CGRect {

    //Decrease frame width of the tableViewCell
    get {
        return super.frame
    }
    set (newFrame) {
        var frame = newFrame
        frame.origin.x += 8
        frame.size.width = frame.size.width-16
        super.frame = frame
    }
}

Below is how the tableViewCell is presented in iOS 11 which is correct iPhone 5s Screenshot

this is how the tableViewCell is presented in iOS 10 which is incorrect

[iPhone 6s screenshot[2]

As you can see the gap from right margin to the tableView Cell is more.

like image 589
MrDank Avatar asked Jan 30 '23 05:01

MrDank


1 Answers

Throw away your existing code. Don't touch the cell's frame in any way.

To make cells that look narrower than the table, give the cell a clear background and put an opaque view inside the content view that is inset from the cell's bounds (or you could use the cell's background view for this). Use autolayout to configure this, so that it works regardless of screen size.

like image 164
matt Avatar answered Jan 31 '23 23:01

matt