iOS工具 - 单项修改Frame成员变量
单项修改成员变量
1 - OC 中是不允许单独修改结构体中成员变量的值,那么我们完全可对 UIView 进行扩展,实现对其尺寸、坐标的单项修改
2 - 代码封装
// - UIView+FrameAddions.h
1 #import <Foundation/Foundation.h> 2 #import <UIKit/UIKit.h> 3 4 @interface UIView (FrameAddions) 5 @property(nonatomic,assign) CGFloat left; 6 @property(nonatomic,assign) CGFloat top; 7 @property(nonatomic,assign) CGFloat right; 8 @property(nonatomic,assign) CGFloat bottom; 9 10 @property(nonatomic,assign) CGFloat width; 11 @property(nonatomic,assign) CGFloat height; 12 13 @property(nonatomic,assign) CGFloat centerX; 14 @property(nonatomic,assign) CGFloat centerY; 15 16 @property(nonatomic,readonly) CGFloat screenX; 17 @property(nonatomic,readonly) CGFloat screenY; 18 @property(nonatomic,readonly) CGFloat screenViewX; 19 @property(nonatomic,readonly) CGFloat screenViewY; 20 @property(nonatomic,readonly) CGRect screenFrame; 21 22 @property(nonatomic) CGPoint origin; 23 @property(nonatomic) CGSize size; 24 @property(nonatomic) BOOL visible; 25 26 /** 27 * Finds the first descendant view (including this view) that is a member of a particular class. 28 */ 29 - (UIView*)descendantOrSelfWithClass:(Class)cls; 30 31 /** 32 * Finds the first ancestor view (including this view) that is a member of a particular class. 33 */ 34 - (UIView*)ancestorOrSelfWithClass:(Class)cls; 35 36 /** 37 * Removes all subviews. 38 */ 39 - (void)removeAllSubviews; 40 41 42 /** 43 * Calculates the offset of this view from another view in screen coordinates. 44 */ 45 - (CGPoint)offsetFromView:(UIView*)otherView; 46 47 48 /** 49 * The view controller whose view contains this view. 50 */ 51 - (UIViewController*)viewController; 52 53 - (void)addSubviews:(NSArray *)views; 54 55 + (UILabel *)labelWithFrame:(CGRect)frame fontSize:(CGFloat)fontSize textAlignment:(NSTextAlignment)alignment text:(NSString *)text; 56 57 @end
// - UIView+FrameAddions.m
1 #import "UIView+FrameAddions.h" 2 3 @implementation UIView (FrameAddions) 4 5 - (BOOL)visible{ 6 return !self.hidden; 7 } 8 9 - (void)setVisible:(BOOL)visible{ 10 self.hidden = !visible; 11 } 12 13 - (CGFloat)left { 14 return self.frame.origin.x; 15 } 16 17 - (void)setLeft:(CGFloat)x { 18 CGRect frame = self.frame; 19 frame.origin.x = x; 20 self.frame = frame; 21 } 22 23 - (CGFloat)top { 24 return self.frame.origin.y; 25 } 26 27 - (void)setTop:(CGFloat)y { 28 CGRect frame = self.frame; 29 frame.origin.y = y; 30 self.frame = frame; 31 } 32 33 - (CGFloat)right { 34 return self.frame.origin.x + self.frame.size.width; 35 } 36 37 - (void)setRight:(CGFloat)right { 38 CGRect frame = self.frame; 39 frame.origin.x = right - frame.size.width; 40 self.frame = frame; 41 } 42 43 - (CGFloat)bottom { 44 return self.frame.origin.y + self.frame.size.height; 45 } 46 47 - (void)setBottom:(CGFloat)bottom { 48 CGRect frame = self.frame; 49 frame.origin.y = bottom - frame.size.height; 50 self.frame = frame; 51 } 52 53 - (CGFloat)centerX { 54 return self.center.x; 55 } 56 57 - (void)setCenterX:(CGFloat)centerX { 58 self.center = CGPointMake(centerX, self.center.y); 59 } 60 61 - (CGFloat)centerY { 62 return self.center.y; 63 } 64 65 - (void)setCenterY:(CGFloat)centerY { 66 self.center = CGPointMake(self.center.x, centerY); 67 } 68 69 - (CGFloat)width { 70 return self.frame.size.width; 71 } 72 73 - (void)setWidth:(CGFloat)width { 74 CGRect frame = self.frame; 75 frame.size.width = width; 76 self.frame = frame; 77 } 78 79 - (CGFloat)height { 80 return self.frame.size.height; 81 } 82 83 - (void)setHeight:(CGFloat)height { 84 CGRect frame = self.frame; 85 frame.size.height = height; 86 self.frame = frame; 87 } 88 89 - (CGFloat)screenX { 90 CGFloat x = 0; 91 for (UIView* view = self; view; view = view.superview) { 92 x += view.left; 93 } 94 return x; 95 } 96 97 - (CGFloat)screenY { 98 CGFloat y = 0; 99 for (UIView* view = self; view; view = view.superview) { 100 y += view.top; 101 } 102 return y; 103 } 104 105 - (CGFloat)screenViewX { 106 CGFloat x = 0; 107 for (UIView* view = self; view; view = view.superview) { 108 x += view.left; 109 110 if ([view isKindOfClass:[UIScrollView class]]) { 111 UIScrollView* scrollView = (UIScrollView*)view; 112 x -= scrollView.contentOffset.x; 113 } 114 } 115 116 return x; 117 } 118 119 - (CGFloat)screenViewY { 120 CGFloat y = 0; 121 for (UIView* view = self; view; view = view.superview) { 122 y += view.top; 123 124 if ([view isKindOfClass:[UIScrollView class]]) { 125 UIScrollView* scrollView = (UIScrollView*)view; 126 y -= scrollView.contentOffset.y; 127 } 128 } 129 return y; 130 } 131 132 - (CGRect)screenFrame { 133 return CGRectMake(self.screenViewX, self.screenViewY, self.width, self.height); 134 } 135 136 - (CGPoint)origin { 137 return self.frame.origin; 138 } 139 140 - (void)setOrigin:(CGPoint)origin { 141 CGRect frame = self.frame; 142 frame.origin = origin; 143 self.frame = frame; 144 } 145 146 - (CGSize)size { 147 return self.frame.size; 148 } 149 150 - (void)setSize:(CGSize)size { 151 CGRect frame = self.frame; 152 frame.size = size; 153 self.frame = frame; 154 } 155 156 - (CGPoint)offsetFromView:(UIView*)otherView { 157 CGFloat x = 0, y = 0; 158 for (UIView* view = self; view && view != otherView; view = view.superview) { 159 x += view.left; 160 y += view.top; 161 } 162 return CGPointMake(x, y); 163 } 164 165 /* 166 - (CGFloat)orientationWidth { 167 return UIInterfaceOrientationIsLandscape(TTInterfaceOrientation()) 168 ? self.height : self.width; 169 } 170 171 - (CGFloat)orientationHeight { 172 return UIInterfaceOrientationIsLandscape(TTInterfaceOrientation()) 173 ? self.width : self.height; 174 } 175 */ 176 - (UIView*)descendantOrSelfWithClass:(Class)cls { 177 if ([self isKindOfClass:cls]) 178 return self; 179 180 for (UIView* child in self.subviews) { 181 UIView* it = [child descendantOrSelfWithClass:cls]; 182 if (it) 183 return it; 184 } 185 186 return nil; 187 } 188 189 - (UIView*)ancestorOrSelfWithClass:(Class)cls { 190 if ([self isKindOfClass:cls]) { 191 return self; 192 } else if (self.superview) { 193 return [self.superview ancestorOrSelfWithClass:cls]; 194 } else { 195 return nil; 196 } 197 } 198 199 - (void)removeAllSubviews { 200 while (self.subviews.count) { 201 UIView* child = self.subviews.lastObject; 202 [child removeFromSuperview]; 203 } 204 } 205 206 - (UIViewController*)viewController { 207 for (UIView* next = [self superview]; next; next = next.superview) { 208 UIResponder* nextResponder = [next nextResponder]; 209 if ([nextResponder isKindOfClass:[UIViewController class]]) { 210 return (UIViewController*)nextResponder; 211 } 212 } 213 return nil; 214 } 215 216 - (void)addSubviews:(NSArray *)views{ 217 for (UIView* v in views) { 218 [self addSubview:v]; 219 } 220 } 221 222 + (UILabel *)labelWithFrame:(CGRect)frame 223 fontSize:(CGFloat)fontSize 224 textAlignment:(NSTextAlignment)alignment 225 text:(NSString *)text{ 226 UILabel *label = [[UILabel alloc] initWithFrame:frame]; 227 label.backgroundColor = [UIColor clearColor]; 228 label.font = [UIFont systemFontOfSize:fontSize]; 229 label.textAlignment = alignment; 230 label.text = text; 231 label.textColor = [UIColor blackColor]; 232 label.lineBreakMode = NSLineBreakByWordWrapping; 233 //return [label autorelease]; 234 return label; 235 } 236 237 @end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)