美文网首页
iOS UIButton 图文上下布局

iOS UIButton 图文上下布局

作者: 冷煖自知 | 来源:发表于2018-03-19 11:55 被阅读88次

    继承UIButton类创建
    在.m中添加方法

    - (void)layoutButtonWithImageTitleSpace:(CGFloat)space {
        CGFloat imageWith = self.imageView.image.size.width;
        CGFloat imageHeight = self.imageView.image.size.height;
        CGFloat labelWidth = self.titleLabel.intrinsicContentSize.width;
        CGFloat labelHeight = self.titleLabel.intrinsicContentSize.height;
        
        UIEdgeInsets imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
        UIEdgeInsets labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        self.titleEdgeInsets = labelEdgeInsets;
        self.imageEdgeInsets = imageEdgeInsets;
    }
    

    在需要的时候调用后,Button会改为上下布局

    听雨阁

    相关文章

      网友评论

          本文标题:iOS UIButton 图文上下布局

          本文链接:https://www.haomeiwen.com/subject/ovguqftx.html