Alter CGRect (or any struct)?

后端 未结 3 1974

I do this quite a bit in my code:

self.sliderOne.frame  = CGRectMake(newX, 0, self.sliderOne.frame.size.width, self.sliderOne.frame.size.height); 

3条回答
  •  北海茫月
    2021-01-06 07:45

    Yeah, you have to do:

    CGRect newFrame = self.sliderOne.frame;
    newFrame.origin.x = frame.size.width - MARGIN * 2 - totalWidth;
    self.sliderOne.frame = newFrame;
    

    It sucks, I know. If you find yourself doing this a lot, you may want to add categories to UIView/NSView to alter this stuff for you:

    @interface UIView (FrameMucking)
    
    - (void) setWidth:(CGFloat)newWidth;
    
    @end
    
    @implementation UIView (FrameMucking)
     - (void) setWidth:(CGFloat)newWidth {
      CGRect f = [self frame];
      f.size.width = newWidth;
      [self setFrame:f];
    }
    @end
    

    Etc.

提交回复
热议问题