ios - UIButton 的 titleEdgeInsets 毫无意义

标签 ios swift uibutton

我有一个 UIButton 的图像和标题。我正在尝试让我的图像(宽度为 15 像素)左对齐,我的标题在其左侧对齐 5 像素:

Button Example

按钮的宽度为 215,但我必须将 titleEdgeInset 设置为 -44。这没有意义。得到 -44 的数学是什么?

最佳答案

这是因为默认情况下按钮的图像和标题居中。

例如,这是一个 300 像素宽的按钮,带有图像和文本,没有插图:

Default button spacing

看起来我需要将按钮内容向左移动 60 磅,所以 -60 左插入和 60 右插入。

button.imageEdgeInsets = UIEdgeInsetsMake(0.0, -60.0, 0.0, 60.0)
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -60.0, 0.0, 60.0)

enter image description here

编辑:

这些插图也适用于上述示例。

button.imageEdgeInsets = UIEdgeInsetsMake(0.0, -120.0, 0.0, 0.0)
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -120.0, 0.0, 0.0)

按钮将其内容居中,因此如果您说“左边还有 2 个点”,按钮会将其内容移动 1 点以使其居中。

规则是

-leftInset+rightInset=leftMargin*2

关于ios - UIButton 的 titleEdgeInsets 毫无意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40667678/

相关文章:

ios - 在 View Controller 转换中获取 subview 的最终帧

ios - iTunes Connect 中未显示新应用

ios - RN Firebase - 如何检查用户是否接受了 IOS 上的通知权限

ios - UINavigationController:确定后退按钮是否被按下或 popViewController 动画

ios - 自动将其中的自定义 UITableViewCell 和 Label 调整为文本

ios - 使用 UIRefreshControl 刷新 TableView 内容

objective-c - 仅在为 UISearchController 按下回车时执行搜索

iphone - iOS 7 UIButton 仅在点击其文本标签时响应

ios - 如何在 Swift 中使用 UIButton 激活 inputView?

iphone - 我如何引用 NSMutableArray 中的 UIButton?