Reputation: 31282
I want to add horizontal padding for my table view cells as the image shows.
I created a subclass of UITableViewCell
, whose width was 314 pixels (screen's width is 320), and set its frame in the initWithCoder
method:
[self customizeXPosition:self.profileImage];
[self customizeXPosition:self.birthdayLabel];
[self customizeXPosition:self.heightLabel];
[self customizeXPosition:self.weightLabel];
this is my - (void)customizeXPosition:(UIView *)view
:
- (void)customizeXPosition:(UIView *)view {
CGRect tmpRect = view.frame;
tmpRect.origin.x += 3.0;
view.frame = tmpRect;
}
The custom cell was smaller than screen, and I moved every element in the cell to right by 3 pixels. I thought this code should achieve my goal, yet it didn't. The view didn't change in the simulator, just like I didn't write any code.
how can I achieve my goal?
Upvotes: 6
Views: 13908
Reputation: 344
Try overriding the -setFrame method in UITableViewCell as per this post :
How to set the width of a cell in a UITableView in grouped style
Upvotes: 6
Reputation: 31282
@faterpig is correct in many situations. However, setting frame may break auto layout.
If your auto layout is broken due to setting frame, you can add a UIView
,which has shorter width than the cell contentView
, to the contentView
and put your other UI elements on the view.
By doing the following settings, you can get cells with "horizontal padding":
cell.backgroundColor = UIColor.clearColor()
cell.contentView.backgroundColor = UIColor.clearColor()
Upvotes: 3
Reputation: 27
This works well for padding things in views:
yourView.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0);
Upvotes: 2