现在我用的修改UIView的位置和大小,都是要重新构造一个CGRect,然后赋值给UIView的frame,即使是只修改一个x值也要构造一个完整的frame,超级麻烦。
我记得看到过有些人的代码是,可以直接在view上修改某个值,就可以生效
UIView *view = [[UIView alloc] init]; view.width = 320.f; view.x = 0.f;
但是我看UIView的属性没有这些,而且frame也不允许修改里边的值,想不通为什么。。。
有没有什么可行的简单方式去修改UIView的大小和位置?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
给UIView加扩展
你说的那人肯定是扩展里加了setWidth:(CGFloat)width
我是这样加的:
以后用的时候直接#import "UIView+FrameMethods.h"
然后[view setWidth:xxx];或view.width = xxx;
UIView+FrameMethods.h
UIView+FrameMethods.m
@implementation UIView (FrameMethods) - (void)moveHorizontal:(CGFloat)horizontal vertical:(CGFloat)vertical { CGRect origionRect = self.frame; CGRect newRect = CGRectMake(origionRect.origin.x + horizontal, origionRect.origin.y + vertical, origionRect.size.width, origionRect.size.height); self.frame = newRect; } - (void)moveHorizontal:(CGFloat)horizontal vertical:(CGFloat)vertical addWidth:(CGFloat)widthAdded addHeight:(CGFloat)heightAdded { CGRect origionRect = self.frame; CGRect newRect = CGRectMake(origionRect.origin.x + horizontal, origionRect.origin.y + vertical, origionRect.size.width + widthAdded, origionRect.size.height + heightAdded); self.frame = newRect; } - (void)moveToHorizontal:(CGFloat)horizontal toVertical:(CGFloat)vertical { CGRect origionRect = self.frame; CGRect newRect = CGRectMake(horizontal, vertical, origionRect.size.width, origionRect.size.height); self.frame = newRect; } - (void)moveToHorizontal:(CGFloat)horizontal toVertical:(CGFloat)vertical setWidth:(CGFloat)width setHeight:(CGFloat)height { CGRect newRect = CGRectMake(horizontal, vertical, width, height); self.frame = newRect; } - (void)setWidth:(CGFloat)width height:(CGFloat)height { CGRect origionRect = self.frame; CGRect newRect = CGRectMake(origionRect.origin.x, origionRect.origin.y, width, height); self.frame = newRect; } - (void)setWidth:(CGFloat)width { CGRect origionRect = self.frame; CGRect newRect = CGRectMake(origionRect.origin.x, origionRect.origin.y, width, origionRect.size.height); self.frame = newRect; } - (void)setHeight:(CGFloat)height { CGRect origionRect = self.frame; CGRect newRect = CGRectMake(origionRect.origin.x, origionRect.origin.y, origionRect.size.width, height); self.frame = newRect; } - (void)addWidth:(CGFloat)widthAdded addHeight:(CGFloat)heightAdded { CGRect originRect = self.frame; CGFloat newWidth = originRect.size.width + widthAdded; CGFloat newHeight = originRect.size.height + heightAdded; CGRect newRect = CGRectMake(originRect.origin.x, originRect.origin.y, newWidth, newHeight); self.frame = newRect; } - (void)addWidth:(CGFloat)widthAdded { [self addWidth:widthAdded addHeight:0]; } - (void)addHeight:(CGFloat)heightAdded { [self addWidth:0 addHeight:heightAdded]; } - (void)setCornerRadius:(CGFloat)radius { [self setCornerRadius:radius borderColor:[UIColor grayColor]]; } - (void)setCornerRadius:(CGFloat)radius borderColor:(UIColor *)borderColor { [self.layer setBackgroundColor:[[UIColor whiteColor] CGColor]]; [self.layer setBorderColor:[borderColor CGColor]]; [self.layer setBorderWidth:1.0]; [self.layer setCornerRadius:radius]; [self.layer setMasksToBounds:YES]; self.clipsToBounds = YES; } - (CGRect)frameInWindow { CGRect frameInWindow = [self.superview convertRect:self.frame toView:self.window]; return frameInWindow; } @endview.frame.size.width
view.frame.origin.x