iOS frame change one property (eg width)

前端 未结 9 1058
日久生厌
日久生厌 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

    If you find yourself needing to do this sort of individual component modification, it may be worthwhile having macros like these somewhere accessible by all of your code:

    #define CGRectSetWidth(rect, w)    CGRectMake(rect.origin.x, rect.origin.y, w, rect.size.height)
    #define ViewSetWidth(view, w)   view.frame = CGRectSetWidth(view.frame, w)
    

    This way, whenever you need to change the width alone - you would simply write

    ViewSetWidth(self, 50);
    

    instead of

    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 50);
    

提交回复
热议问题