Objective c UIButton插件背后的逻辑(sizeToFit)

Objective c UIButton插件背后的逻辑(sizeToFit),objective-c,ios8,uibutton,Objective C,Ios8,Uibutton,我以编程方式创建UIButton。我期待着调用sizeToFit根据标题大小(和字体)调整我的按钮大小。但它显示了一个插图,正如标题上方和下方的灰色区域所示 所有插入值显示为0.0(因为它们应该用UIEdgeInsetsZero初始化) 我肯定我错过了什么。在哪里可以找到这些插入值 (这是iOS 8.3的一部分) 更新sizeToFit 一些评论让我更加具体:我知道contentEdgeInsets的默认值是UIEdgeInsetsZero,正如苹果文档中所述。我的帖子表明,在某种程度上,应用

我以编程方式创建UIButton。我期待着调用
sizeToFit
根据标题大小(和字体)调整我的按钮大小。但它显示了一个插图,正如标题上方和下方的灰色区域所示

所有插入值显示为0.0(因为它们应该用UIEdgeInsetsZero初始化)

我肯定我错过了什么。在哪里可以找到这些插入值

(这是iOS 8.3的一部分)

更新sizeToFit

一些评论让我更加具体:我知道
contentEdgeInsets
的默认值是UIEdgeInsetsZero,正如苹果文档中所述。我的帖子表明,在某种程度上,应用了不同的价值观。 我用两个“空”按钮控件进行了测试:

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

一些内部逻辑调整了大小,我找不到这个值的来源。它不会反映在ContentEdgeInSet中。

它在按钮周围增加了一些额外的间距

试试这个

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
这是一个bug(21437476),已经修复:

苹果开发者关系16-Sep-2015 09:51 PM 我们认为这个问题已经得到解决。请使用iOS 9 GM(内部版本:13A344)验证此问题,并使用结果更新您的错误报告

iOS 9通用汽车公司(内部版本:13A344)

发布日期:2015年9月16日


它也处于打开状态。

确实,显式设置contentEdgeInsets似乎很有效……除非它是0(或UIEdgeInsetsZero),并且此行为没有记录在案。我可以使用
UIEdgeInsetsMake(0.0,0.0,1.0,0.0)
只在底部获得一行像素,但我仍然想知道这个默认填充是从哪里来的。请检查URL:default velue是UIEdgeInsetsZero。谢谢,我确实尝试过这个。它似乎只与
contentedgeinset
相关。
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