IOS - adding horizontal strip to table cells

enter image description here

I want to add horizontal padding for my table cells as shown.

I created a subclass of UITableViewCell whose width was 314 pixels (screen width - 320), and set its frame in the initWithCoder :

 [self customizeXPosition:self.profileImage]; [self customizeXPosition:self.birthdayLabel]; [self customizeXPosition:self.heightLabel]; [self customizeXPosition:self.weightLabel]; 

this is mine - (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 the screen, and I moved each element in the cell 3 pixels to the right. I thought this code should achieve my goal, but it is not. The view has not changed in the simulator, just as I did not write any code.

How can I achieve my goal?

+6
source share
3 answers

Try overriding the -setFrame method in a UITableViewCell according to this post:

How to set cell width in UITableView in grouped style

version of Swift

+7
source

This works well to populate things in views:

yourView.layer.sublayerTransform = CATransform3DMakeTranslation (10, 0, 0);

+3
source

@faterpig is correct in many situations. However, setting the frame may interfere with the automatic layout.

If your automatic layout is broken due to the setting of the frame, you can add a UIView that is shorter than the contentView cell in the contentView and put other user interface elements in the view.

Performing the following settings, you can get cells with "horizontal laying":

 cell.backgroundColor = UIColor.clearColor() cell.contentView.backgroundColor = UIColor.clearColor() 
+3
source

Source: https://habr.com/ru/post/956915/


All Articles