how do I set the size/position of a custom UIView

2019-04-15 07:14发布

问题:

How do I set the size of a subview which is being placed programmatically inside a UITableViewCell?

I have a custom (subclassed) UITableViewCell in which part of the cell includes a number of rows (dynamic) which are implemented as a custom UIView. The idea is to create the custom view (which is a row of items) and then when the UITableViewCell hits layoutSubviews it will (a) set itself up re positioning and (b) loop through and call "layoutSubviews" on each of the rows of the custom UIViews it is using for the rows.

Question - I'm not sure how to correctly set the size/position of the custom UIView row? How do I, within the custom UIView layoutSubviews method, determine its x,y,width,height so I can position it?

Do I actually for example need to manually, within the UITableCellView's layoutSubviews method, loop through all the rows and call a custom method of them to pass them their position/width/height's that they will need to be at? Then when the custom UIView row's layoutSubview method is hit it would know to look up it's instance variable which stored these values to use them?

Hopefully this makes sense.

Structure is:

Dynamic Custom UITableCellView (subclassed)

  • determines rowHeight dynamically in heightForRowAtIndexPath
  • in layoutSubviews it includes looping through subviews (of custom UIView I have) and calls layoutSubview on each of these

In custom UIView (which represents a row of items, e.g. UILabel)

  • in layoutSubview - QUESTION: How do set the size of the frame of this subview?
  • I assume the x,y for the frame should be 0,0?
  • For the width/height how do you get the full size of the superview area to which it should set in?
  • Should the superview have passed this information down to it prior to the layoutSubviews?

回答1:

any views added as part of the tableViewCell subclass are positioned relative to the cells frame, ie x:0,y:0 for the subview origin, would be the top left corner of the tableCell. Something like this should be enough to get you started.

CGRect frame =[self frame];
frame.size.height=20.0f;
frame.origin.x=(self.frame.size.height/2)-frame.size.height;
frame.origin.y=0.0f;
[subview setFrame:frame];


回答2:

You can add a category to UIView with resize methods.

UIView+ResizeMethods.h

- (void) setHeight: (CGFloat) height;
- (void) setWidth: (CGFloat) width;
- (void) setTop: (CGFloat) top;
- (void) setLeft: (CGFloat) left;

UIView+ResizeMethods.m

- (void) setHeight: (CGFloat) height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
- (void) setWidth: (CGFloat) width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
- (void) setTop: (CGFloat) top {
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}
- (void) setLeft: (CGFloat) left {
    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;
}