clipsToBounds在UIStackView中似乎没有任何作用 [英] clipsToBounds seeming to have no effect in UIStackView

查看:202
本文介绍了clipsToBounds在UIStackView中似乎没有任何作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在UIStackView之外进行子视图渲染?我有一个带有按钮和标签的垂直堆栈视图,并且我正在使用堆栈视图的宽度来适当地设置按钮的高度和宽度:

Is it possible to have a subview render outside a UIStackView? I have a vertical stack view with a button and a label, and I'm using the width of the stack view to set the button's height and width appropriately:

addArrangedSubview(self.loginMethodButton)
let widthConstraint = NSLayoutConstraint(item: self.loginMethodButton, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1.0, constant: 0.0)
let heightConstraint = NSLayoutConstraint(item: self.loginMethodButton, attribute: .height, relatedBy: .equal, toItem: self.loginMethodButton, attribute: .width, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([widthConstraint, heightConstraint])

然后我根据情况将viewController中的宽度调整为适当的大小.问题在于,在按钮需要使用的较小尺寸之一中,宽度小于其下方的标签,从而切断了标签.

I then adjust the width in my viewController to appropriate sizes based on the situation. The problem is, in one of the smaller sizes the button needs to be, the width is smaller than the label beneath it, which cuts off the label.

如果我在设置标签时调用sizeToFit(),它可以工作,但是,在设置标签后调整尺寸时调用sizeToFit()仍然会导致标签被切断.在标签视图和堆栈视图上将clipsToBounds设置为false均无效.

If I call sizeToFit() when setting the label, it works, however, calling sizeToFit() when adjusting the sizes after the label has been set still results in the label being cut off. Setting clipsToBounds to false on both the label and stack view have no effect.

是否有可能在堆栈视图之外渲染视图?还是我必须找到一种方法来重新处理我的约束条件才能解决此问题?

Is it possible to render a view outside of a stack view at all? Or am I going to have to find a way to re-work my constraints in order to fix this issue?

推荐答案

UIStackView是一个非渲染"对象,它所做的只是排列其他视图.

UIStackView is a "non-rendering" object... all it does is arrange other views.

如果问题是您的UILabel太宽而无法容纳Stack View可用的区域,则您需要将标签设置为自动缩小"字体,或者需要更改字体堆栈视图约束可以使用更宽的标签.

If the problem is that your UILabel is too wide to fit in the area made available by your Stack View, then you either need to set the label to "auto-shrink" the font, or you need to alter your Stack View constraints to allow for the wider label.

编辑 根据您的其他评论,以下内容不正确.

您需要在包含"视图中设置.clipsToBounds ...基于所显示的图形,这将是一个深蓝色的框(我假设这是带有图像的UIView标签作为子视图.

You need to set .clipsToBounds on the "containing" view... based on the graphic you are showing, that would be the darker-blue box (I'm assuming that is a UIView with the image and label as subviews).

这篇关于clipsToBounds在UIStackView中似乎没有任何作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆