简体   繁体   English

UIView动画中的UIButton字体大小问题

[英]UIButton font size issue inside UIView animation

I have a strange issue with UIButton s inside a UIView. 我在UIView内的UIButton遇到一个奇怪的问题。 The view is initially constrained to a height of 0, and I'm animating the constraint using 该视图最初被约束为0的高度,并且我使用

UIView.animateWithDuration(0.6, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {

        heightConstraint!.constant = height
        self.view.layoutIfNeeded()

    }) { (finished) in

    }

This works really well, with all the content of the animated view appearing as expected, apart from any UIButtons that are placed inside. 这非常好用,动画视图的所有内容均按预期显示,除了放置在其中的任何UIButton之外。

The buttons' titleLabel font size increases from 0 to the correct size during the animation . 在动画过程中,按钮的titleLabel字体大小从0增加到正确的大小 There's nothing special about these buttons; 这些按钮没有什么特别的。 they are set to system font of 18, positioned relative to the superview. 它们设置为相对于超级视图定位的系统字体18。 The button frames are correct, its just the font size that changes. 按钮框是正确的,只是字体大小发生了变化。

I've tried explicitly setting adjustsFontSizeToFitWidth and contentMode but these have no effect. 我已经尝试过显式设置adjustsFontSizeToFitWidthcontentMode但是它们没有效果。 Interestingly, if I add a plain UILabel to the view, this works just fine. 有趣的是,如果我在视图中添加一个普通的UILabel ,则效果很好。

Any suggestions as to what is going on? 有什么建议吗?

I'm managed to avoid the issue by explicitly setting the height to a non-zero value before the animation: 我设法通过在动画之前将高度显式设置为非零值来避免该问题:

constraint.constant = 1
self.view.layoutIfNeeded()
self.view.setNeedsUpdateContraints()

// do animation

This resolves the issue, although it feels like a hack around something I'm not doing correctly in the first place. 尽管可以感觉到我一开始就无法正确执行某些操作,但是这可以解决问题。

Try with clipsToBounds = true , Text is hiding inside the button 尝试使用clipsToBounds = true ,文本隐藏在按钮内

When height is 0 or 1, myView is 当height为0或1时,myView为

在此处输入图片说明

and after setting clipsToBounds = true , its like this 在设置clipsToBounds = true ,它像这样

在此处输入图片说明

Note Border = 1 and height = 1 is just to represent here. 注意 Border = 1和height = 1仅代表此处。 Even I'm getting this strange issue. 甚至我也遇到了这个奇怪的问题。 But the text do not display after setting the bounds to true 但是将边界设置为true后,文本不会显示

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

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