iOS frame change one property (eg width)

前端 未结 9 1033
日久生厌
日久生厌 2020-12-14 00:20

This question was originally asked for the objective-c programming language. At the time of writing, swift didn\'t even exist yet.

Question

Is it po

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 00:59

    Hope I am not too late to the party, here's my solution in ObjC.

    For me I prefer to stick to a single function instead of several functions, and I like the CSS approach shown by the poster. Below is the function I add to my UIView Category.

    - (void)resetFrame:(CGRect)frame {
    CGRect selfFrame = self.frame;
    selfFrame.origin = CGPointMake(frame.origin.x>0 ? frame.origin.x : selfFrame.origin.x, frame.origin.y>0 ? frame.origin.y : selfFrame.origin.y);
    selfFrame.size = CGSizeMake(frame.size.width>0 ? frame.size.width : selfFrame.size.width, frame.size.height>0 ? frame.size.height : selfFrame.size.height);
    [self setFrame:selfFrame]; }
    

    Shown in the example, it looks at each values in the provided CGRect, and if the value is more than 0, it means we want to replace that value. And therefore, you can do CGRect(0,0,100,0) and it will assume we want to replace the width only.

提交回复
热议问题