首页 > 解决方案 > 是什么干扰了 UIButton 内的自定义插图?

问题描述

当我注意到这种令人惊讶的行为时,我正在使用John Sundell 的Applying rounded corners to a UIKit or SwiftUI view in a Playground 中的代码,零边缘插入设置确实会导致 UIButton 内部有一些 y 轴填充,而添加严格的正 x -axis insets 确实删除了 y 轴填充。知道是什么解释了这种行为吗?

0-inset 导致 y 轴填充

添加一些 x 轴插图会取消 y 轴填充

标签: iosxcodeuibuttonuikit

解决方案


来自 Apple 的UIButton .contentEdgeInsets文档:

... 默认值为零。

该按钮使用此属性来确定intrinsicContentSize 和sizeThatFits(_:)。

虽然不清楚,如果.contentEdgeInsets它们是默认的,UIKit 会添加默认的顶部和底部“填充”。

如果任何值不是默认值零,UIKit 会假定您已完全按照您的需要设置这些值。


推荐阅读