简体   繁体   English

UIButton占据了自己的尺寸Autolayout

[英]UIButton takes up its own size Autolayout

What I tried was this :- 我试过的是: -

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:btn];
btn.translatesAutoresizingMaskIntoConstraints = NO;
[btn addTarget:self action:@selector(bringUpNextViewController:) 
                    forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
[btn setTitle:@"8" forState:UIControlStateNormal];
[self.view layoutIfNeeded];

NSLog(@"button size : %@", NSStringFromCGSize(btn.frame.size));

As output, I get this : 作为输出,我得到这个:

button size : {30, 29}

Then I gave setTitle string as nothing. 然后我给了setTitle字符串什么都没有。 The button width was still 30. 按钮宽度仍为30。

So why is this the case always? 那么为什么总是这样呢?

I also tried giving a high compression resistance priority and high content hugging priority . 我还尝试给出high compression resistance priorityhigh content hugging priority Doesn't shrink to nothing. 不缩减为零。

The problem is also the fact that I want to reduce the width of the button simply based on its content, without giving any fixed width. 问题还在于我想简单地根据其内容减小按钮的宽度,而不给出任何固定的宽度。

I could take the width of text and give the button the width, but I shouldn't be needing to do that either if the button was taking up the content width. 我可以获取文本的宽度并给按钮宽度,但如果按钮占用内容宽度,我不应该这样做。

EDIT: 编辑:

Its not the insets either which is causing the width to be 30 . 它不是导致宽度为30的插入物。 Ghost value. 幽灵的价值。

A button is made of several subviews. 按钮由多个子视图组成。 It's very likely that the internal layout of a button has some default padding between the label and the button view itself. 按钮的内部布局很可能在标签和按钮视图本身之间有一些默认填充。

Making a button like yours and examining the constraints shows the following: 制作像您这样的按钮并检查约束显示以下内容:

button constraints (
"<NSContentSizeLayoutConstraint:0x8c40a60 H:[UIButton:0x8f29840(30)] Hug:250 CompressionResistance:750>",
"<NSContentSizeLayoutConstraint:0x8c55280 V:[UIButton:0x8f29840(29)] Hug:250 CompressionResistance:750>"
)

The 30 and 29 tie up with the size values you are seeing. 30和29与您看到的大小值相关联。 The intrinsic content size property of the button also returns 30,29. 按钮的内在内容大小属性也返回30,29。 Basically this is the minimum size for a button, in the absence of anything else. 基本上这是按钮的最小尺寸,没有任何其他东西。

It's not quite clear what you want or why you are bothered by this. 目前还不是很清楚你想要什么,或者为什么你会为此烦恼。 Anything smaller will be a poor touch target, and a button with no label or image will be invisible anyway. 任何较小的东西都将是一个糟糕的触摸目标,无论如何,没有标签或图像的按钮将是不可见的。 If you add a longer title, the button will get bigger. 如果添加更长的标题,按钮将变大。 If you add other constraints to force particular sizes, then these will override the intrinsic content size. 如果添加其他约束以强制特定大小,则这些将覆盖内在内容大小。

If you want the button to become invisible when it has no title, then you should explicitly hide it. 如果你希望按钮在没有标题时变得不可见,那么你应该明确地隐藏它。 This makes your intentions in the code much clearer and will prevent the user from accidentally hitting a button they can't really see. 这使得您在代码中的意图更加清晰,并且可以防止用户意外地点击他们无法看到的按钮。

I'm wondering if there is a minimum intrinsic content size for a uibutton? 我想知道uibutton是否有最小的内在内容大小?

Anyway, try doing... 无论如何,试着做...

[button invalidateIntrinsicContentSize];

Did you try [button sizeToFit]; 你试过[button sizeToFit]; ?

For custom buttons, I think that you will need to override: 对于自定义按钮,我认为您需要覆盖:

- (CGSize)sizeThatFits:(CGSize)size;

Finally, if nothing other works, you can always try giving the button width from the text size like so 最后,如果没有其他工作,你总是可以尝试从文本大小给出按钮宽度,如此

CGSize textsize = [yourText sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:14]]; 
[button setFrame:CGRectMake(0,0,textsize.width, textsize.height)];

First define a constraint for button size in storyboard. 首先在故事板中定义按钮大小的约束。

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonSizeConst;

After that you can set it's size to whatever you want like this. 之后,你可以将它的大小设置为你想要的任何东西。

self.buttonSizeConst.constant = 65.0;

在此输入图像描述

Edit: With this method you need to calculate your button width but I think you don't want to do that. 编辑:使用此方法,您需要计算您的按钮宽度,但我认为您不想这样做。 You need to autoresize UIButton for it's content. 您需要为其内容自动调整UIButton。 For this you should give constraints like image below. 为此,您应该给出如下图所示的约束。 It will expand to right when you change your title. 当您更改标题时,它将扩展到右侧。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM