UIButton insets (sizeToFit) 背后的逻辑
Logic behind a UIButton insets (sizeToFit)
我以编程方式创建了一个 UIButton。我期待调用 sizeToFit
来调整我的按钮的标题大小(和字体)。但是它显示了一个插图,可以看到标题上方和下方的灰色区域。
所有 insets 值显示 0.0(因为它们应该使用 UIEdgeInsetsZero 进行初始化)
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"Tap Me" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor darkGrayColor];
btn.titleLabel.backgroundColor = [UIColor blueColor];
[btn sizeToFit]; // Resize will set the width and height
[self.view addSubview:btn];
NSLog(@"%.2f %.2f %.2f", btn.imageEdgeInsets.top, btn.contentEdgeInsets.top, btn.titleEdgeInsets.top);
// Previous statement output :
// ... Sandbox[1764:59117] 0.00 0.00 0.00
}
我确定我遗漏了什么。我在哪里可以找到这些插图值?
(这是 iOS 8.3)
关于 sizeToFit 的更新
一些评论让我更具体:我知道 contentEdgeInsets
的默认值是 UIEdgeInsetsZero,如苹果文档中所述。我的 post 建议以某种方式应用不同的值。
我测试了两个 "empty" UIButton 控件:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor darkGrayColor];
[btn sizeToFit];
[self.view addSubview:btn];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
btn2.backgroundColor = [UIColor purpleColor];
[btn2 sizeToFit];
[self.view addSubview:btn2];
// Put some code next to move centers to display buttons properly
// ...
- UIButtonTypeCustom(上图灰色背景):调用
sizeToFit
后控件大小为30x34
- UIButtonTypeSystem(紫色背景):30x30
一些内部逻辑调整大小,我找不到这个值来自哪里。它没有反映在 contentEdgeInsets 中。
它在按钮周围添加了一些额外的间距
试试这个
btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
它将删除插图。
希望对您有所帮助。
imageEdgeInsets、contentEdgeInsets、titleEdgeInsets 的默认值为 UIEdgeInsetsZero。
请使用以下代码:
self.view.backgroundColor = [UIColor lightGrayColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"Tap Me" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor darkGrayColor];
btn.titleLabel.backgroundColor = [UIColor blueColor];
[btn sizeToFit]; // Resize will set the width and height
NSLog(@"%.2f %.2f %.2f", btn.imageEdgeInsets.top, btn.contentEdgeInsets.top, btn.titleEdgeInsets.top);
// Previous statement output :
// ... Sandbox[1764:59117] 0.00 0.00 0.00
btn.contentEdgeInsets = UIEdgeInsetsMake( 10.0, 0.0, 0.0, 0.0);
[self.view addSubview:btn];
NSLog(@"%.2f %.2f %.2f", btn.imageEdgeInsets.top, btn.contentEdgeInsets.top, btn.titleEdgeInsets.top);
// Previous statement output :
// 0.00 10.00 0.00
这是一个错误 (21437476),现已修复:
Apple Developer Relations16-Sep-2015 09:51 PM
We believe this issue has been addressed. Please verify this issue with iOS 9 GM (Build: 13A344) and update your bug report at http://bugreport.apple.com/ with the results.
iOS 9 GM (Build: 13A344)
https://developer.apple.com/ios/download/
Posted Date: Sept. 16th, 2015
它也在 Open Radar 上。
我以编程方式创建了一个 UIButton。我期待调用 sizeToFit
来调整我的按钮的标题大小(和字体)。但是它显示了一个插图,可以看到标题上方和下方的灰色区域。
所有 insets 值显示 0.0(因为它们应该使用 UIEdgeInsetsZero 进行初始化)
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"Tap Me" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor darkGrayColor];
btn.titleLabel.backgroundColor = [UIColor blueColor];
[btn sizeToFit]; // Resize will set the width and height
[self.view addSubview:btn];
NSLog(@"%.2f %.2f %.2f", btn.imageEdgeInsets.top, btn.contentEdgeInsets.top, btn.titleEdgeInsets.top);
// Previous statement output :
// ... Sandbox[1764:59117] 0.00 0.00 0.00
}
我确定我遗漏了什么。我在哪里可以找到这些插图值?
(这是 iOS 8.3)
关于 sizeToFit 的更新
一些评论让我更具体:我知道 contentEdgeInsets
的默认值是 UIEdgeInsetsZero,如苹果文档中所述。我的 post 建议以某种方式应用不同的值。
我测试了两个 "empty" UIButton 控件:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor darkGrayColor];
[btn sizeToFit];
[self.view addSubview:btn];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
btn2.backgroundColor = [UIColor purpleColor];
[btn2 sizeToFit];
[self.view addSubview:btn2];
// Put some code next to move centers to display buttons properly
// ...
- UIButtonTypeCustom(上图灰色背景):调用
sizeToFit
后控件大小为30x34 - UIButtonTypeSystem(紫色背景):30x30
一些内部逻辑调整大小,我找不到这个值来自哪里。它没有反映在 contentEdgeInsets 中。
它在按钮周围添加了一些额外的间距
试试这个
btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
它将删除插图。
希望对您有所帮助。
imageEdgeInsets、contentEdgeInsets、titleEdgeInsets 的默认值为 UIEdgeInsetsZero。
请使用以下代码:
self.view.backgroundColor = [UIColor lightGrayColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"Tap Me" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor darkGrayColor];
btn.titleLabel.backgroundColor = [UIColor blueColor];
[btn sizeToFit]; // Resize will set the width and height
NSLog(@"%.2f %.2f %.2f", btn.imageEdgeInsets.top, btn.contentEdgeInsets.top, btn.titleEdgeInsets.top);
// Previous statement output :
// ... Sandbox[1764:59117] 0.00 0.00 0.00
btn.contentEdgeInsets = UIEdgeInsetsMake( 10.0, 0.0, 0.0, 0.0);
[self.view addSubview:btn];
NSLog(@"%.2f %.2f %.2f", btn.imageEdgeInsets.top, btn.contentEdgeInsets.top, btn.titleEdgeInsets.top);
// Previous statement output :
// 0.00 10.00 0.00
这是一个错误 (21437476),现已修复:
Apple Developer Relations16-Sep-2015 09:51 PM We believe this issue has been addressed. Please verify this issue with iOS 9 GM (Build: 13A344) and update your bug report at http://bugreport.apple.com/ with the results.
iOS 9 GM (Build: 13A344) https://developer.apple.com/ios/download/
Posted Date: Sept. 16th, 2015
它也在 Open Radar 上。