How to determine margin of a grouped UITableView (or better, how to set it)?

前端 未结 9 2416
暗喜
暗喜 2020-12-07 17:48

The grouped UITableView places a margin between the edge of the view and the table cells. Annoyingly (for me) this margin is some function of the width of the view.

9条回答
  •  天命终不由人
    2020-12-07 18:36

    I was using Matthew Thomas implementation but it's unreliable (it's broken if you use autorotation in your controllers) and has a lot of hardcoded code... I realized that there is a very simple solution, my implementation is a single line of code:

    - (float)cellMargins
    {
        return self.backgroundView.frame.origin.x * 2;
    }
    

    This is far better IMO :)

    EDIT: my implementation was unreliable too (it does not work properly when switching to/from editing mode), this is my final implementation (I handled right and left margins separately):

    - (float)leftMargin
    {
        return self.contentView.frame.origin.x;
    }
    
    - (float)rightMargin
    {
        CGRect frame = self.contentView.frame;
        float containerWidth = frame.size.width;
        float margin = self.frame.size.width - (containerWidth + frame.origin.x);
        return margin;
    }
    
    - (float)cellMargins
    {
        return ([self leftMargin] + [self rightMargin]);
    }
    

提交回复
热议问题