【问题标题】:How to use UIEdgeInsets property in Button in iphone如何在 iphone 的 Button 中使用 UIEdgeInsets 属性
【发布时间】:2009-11-23 05:27:21
【问题描述】:

我是 iOS 应用程序开发的新手,想知道如何在按钮中使用 UIEdgeInsets 属性,所以请指导我。

【问题讨论】:

    标签: ios iphone uiedgeinsets


    【解决方案1】:

    首先,您的问题对于您要达到的目标不是很清楚,所以...问一个一般性问题,得到一个一般性答案。

    UIButton 具有三个 UIEdgeInsets 类型的属性——contentEdgeInsetstitleEdgeInsetsimageEdgeInsets。您可以使用这些属性为所有按钮内容分配特定的插图,包括标题和图像,或者单独分配一个或另一个。

    您使用UIEdgeInsetsMake() 函数创建 UIEdgeInsets 值。 UIEdgeInsets 分别为顶部、左侧、底部和右侧(分别)的 inset 值封装了四个不同的 CGFloat 值。正 inset 值会缩小内容区域,而负 inset 值会有效增加内容区域。

    如果您对如何将 UIEdgeInsets 与 UIButton 一起使用有更具体的问题,我建议您重新考虑如何提问并重试。 :)

    编辑(解决评论中的问题): 听起来您会想要执行以下操作:

    UIEdgeInsets titleInsets = UIEdgeInsetsMake(0.0, 7.0, 0.0, 0.0);
    button.titleEdgeInsets = titleInsets;
    

    【讨论】:

    【解决方案2】:

    如果你想要一个扩展按钮并简单地使用,使用这个

    button.setInset(上:0.0,左:7.0,下:0.0,右:0.0)

    extension UIButton {
        /**
            Creates an edge inset for a button
            :param: top CGFLoat
            :param: left CGFLoat
            :param: bottom CGFLoat
            :param: right CGFLoat
        */
        func setInset(#top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
               self.titleEdgeInsets =  UIEdgeInsetsMake(top, left, bottom, right)
        }
    }
    

    【讨论】:

      【解决方案3】:

      请注意,有时您不需要设置 contentEdgeInsets。只需给按钮更大的高度和宽度约束。标题本身将自动居中。

      【讨论】:

        猜你喜欢
        • 2021-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多