TIMEX
TIMEX

Reputation: 271634

How do I make the UITableView separator inset go from the edges of the screen?

class RoomNewTableViewCell: UITableViewCell {

    var row:Int?
    var room: Room?{
        didSet{
            updateUI() //this is when render is called
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.layoutMargins = UIEdgeInsetsZero 
        self.separatorInset = UIEdgeInsetsMake(0, -15, 0, 0) 
    }



    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func updateUI(){
        println("Updating UI")
        let test = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        test.backgroundColor = UIColor.greenColor()
        self.backgroundColor = UIColor.blueColor()
        self.addSubview(test)
    }

}

}

This is how I create my tableView:

    //Draw Table
    self.roomsTableView.delegate = self
    self.roomsTableView.dataSource = self
    self.roomsTableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
    self.roomsTableView.separatorColor = UIColor(hex: 0xededed)
    let tableViewStartY = CGFloat(0) //used to be 20.0
    let extraPaddingToAlignTable = CGFloat(50)
    self.roomsTableView.frame = CGRectMake(0, tableViewStartY, screenWidth, CGRectGetMinY(self.tabBarController!.tabBar.frame) + extraPaddingToAlignTable)
    self.roomsTableView.addSubview(self.refreshControl)
    self.roomsTableView.tableFooterView = UIView(frame: CGRectZero)


    self.roomsTableView.rowHeight = CGFloat(100)
    self.roomsTableView.clipsToBounds = true
    self.roomsTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
    self.roomsTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
    self.roomsTableView.registerClass(RoomNewTableViewCell.self, forCellReuseIdentifier: "newCell")
    self.view.addSubview(self.roomsTableView)

I've tried playing around separatorInset for both the table and cell, but can't seem to make it work.

enter image description here

Upvotes: 1

Views: 2576

Answers (1)

technerd
technerd

Reputation: 14504

Since launch of iOS 8.0, apple added layoutMargins property for UITableViewCell and UITableView.

So you can set layoutMargin to zero in willDisplayCell: delegate method of TableView. This code just set tableview layoutMargin and cell layoutMargin as UIEdgeInsetsZero .

This will work for iOS 7.0 also.

Try this may help you.

Swift

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        if(tableView.respondsToSelector("setSeparatorInset:")){

            tableView.separatorInset = UIEdgeInsetsZero
        }

        if(tableView.respondsToSelector("setLayoutMargins:")){

            tableView.layoutMargins = UIEdgeInsetsZero
        }

        if(cell.respondsToSelector("setLayoutMargins:")){

            cell.layoutMargins = UIEdgeInsetsZero
        }

    }

Objective C

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

   if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
   }
}

For iOS 9.0, you have to set cellLayoutMarginsFollowReadableWidth to NO.

Objective C

- (void)viewDidLoad {
    [super viewDidLoad];

    //For iOS 9 and Above 

    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 9.0) {
        self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
    }
}

Swift

override func viewDidLoad() {
        super.viewDidLoad()

        //For iOS 9 and Above 
        if #available(iOS 9, *) {
            tableView.cellLayoutMarginsFollowReadableWidth = false
        }
    }

You can also set this value from Storyboard.

Open Storyboard and go to Attribute Inspector for TableView Cell.

Change type of Separator field to Custom Insets and change value of Left to 0.

Please find below image.

enter image description here

Upvotes: 4

Related Questions