UIView(含所有子视图)分类方法 --- 为了方便设置控件的frame

部分代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
//设置控件frame时,先设置尺寸width,height,再设置中心,centerX,centerY<br>- (CGFloat)sth_right{
    return self.sth_x+ self.sth_width;
}
- (void)setSth_right:(CGFloat)sth_right{
     
    self.sth_x = sth_right - self.sth_width;
}
- (CGFloat)sth_bottom{
    return self.sth_y+ self.sth_height;
}
- (void)setSth_bottom:(CGFloat)sth_bottom{
    self.sth_y = sth_bottom - self.sth_height;
}

 实例分析:

 1.注意一般设置子view位置时,一般使用width或height,及x,y,最好不要使用centerX或Y一般说来不太准确。

 2.分类设置子view位置时一定要重写layoutSubViews方法,并先调用父类方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//view分类
#import <UIKit/UIKit.h>
@interface UIView (STHViewFrame)
@property (nonatomic, assign) CGFloat sth_x;
@property (nonatomic, assign) CGFloat sth_y;
@property (nonatomic, assign) CGFloat sth_w;
@property (nonatomic, assign) CGFloat sth_h;
@property (nonatomic, assign) CGFloat sth_centerX;
@property (nonatomic, assign) CGFloat sth_centerY;
@property (nonatomic, assign) CGFloat sth_right;
@property (nonatomic, assign) CGFloat sth_bottom;
@end
 
#import "UIView+STHViewFrame.h"
 
@implementation UIView (STHViewFrame)
 
- (CGFloat)sth_x{
    return self.center.x;
}
- (void)setSth_x:(CGFloat)sth_x{
     
    CGRect frame  = self.frame;
    frame.origin.x = sth_x;
    self.frame = frame;
}
- (CGFloat)sth_y{
    return self.center.y;
}
- (void)setSth_y:(CGFloat)sth_y{
    CGRect frame = self.frame;
    frame.origin.y = sth_y;
    self.frame = frame;
}
- (CGFloat)sth_w{
    return self.frame.size.width;
}
- (void)setSth_w:(CGFloat)sth_w{
    CGRect frame = self.frame;
    frame.size.width = sth_w;
    self.frame = frame;
}
- (CGFloat)sth_h{
    return self.frame.size.height;
}
- (void)setSth_h:(CGFloat)sth_h{
    CGRect frame = self.frame;
    frame.size.height = sth_h;
    self.frame = frame;
}
- (CGFloat)sth_centerX{
    return self.center.x;
}
- (void)setSth_centerX:(CGFloat)sth_centerX{
     
    CGPoint center = self.center;
    center.x = sth_centerX;
    self.center = center;
}
- (CGFloat)sth_centerY{
    return self.center.y;
}
- (void)setSth_centerY:(CGFloat)sth_centerY{
     
    CGPoint center = self.center;
    center.y = sth_centerY;
    self.center = center;
}
- (CGFloat)sth_right{
     
    return self.sth_x + self.sth_w;
}
- (void)setSth_right:(CGFloat)sth_right{
    self.sth_x = sth_right - self.sth_w;
}
- (CGFloat)sth_bottom{
     
    return self.sth_y + self.sth_h;
}
- (void)setSth_bottom:(CGFloat)sth_bottom{
    self.sth_y = sth_bottom - self.sth_h;
}
@end
 
//自定义按钮及布局
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
@interface customButton : UIButton
@end
 
#import "ViewController.h"
#import "UIView+STHViewFrame.h"
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     
    NSArray *images = @[@"mine_icon_nearby",@"mine_icon_random",@"mine-icon-activity"];
    NSArray *titles = @[@"位置",@"我的",@"他的"];
     
    for (NSInteger i = 0; i < images.count; i++) {
         
        customButton *button = [[customButton alloc] initWithFrame:CGRectMake(20+i*110, 200, 100, 100)];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor yellowColor]];
        [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:button];
    }
}
@end
@implementation customButton
 
- (void)layoutSubviews{
    [super layoutSubviews];
    NSLog(@"%f%f--%f%f",self.sth_centerY,self.sth_centerX,self.sth_h,self.sth_w);
    self.imageView.sth_centerX = self.sth_w/2;
    self.imageView.sth_y = self.sth_h/2 - 5 - self.imageView.image.size.height/2;
    self.imageView.sth_w = self.imageView.image.size.width;
    self.imageView.sth_h = self.imageView.image.size.height;
    self.titleLabel.sth_y = CGRectGetMaxY(self.imageView.frame)+5;
    self.titleLabel.sth_x = 0;
    self.titleLabel.sth_h = 17;
    self.titleLabel.sth_w = self.sth_w;
}
@end

  效果图:

 

posted @   TheYouth  阅读(477)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示