欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios 开发中UIBottom的image的上下左右方向排列实现方法

程序员文章站 2022-04-12 20:13:26
ios 开发中uibottom的image的上下左右方向排列实现方法,效果图: 主要代码:继承于uibottom,主要修改titleedgeinsets和imageedgeinsets 属性 cu...

ios 开发中uibottom的image的上下左右方向排列实现方法,效果图:

ios 开发中UIBottom的image的上下左右方向排列实现方法

主要代码:继承于uibottom,主要修改titleedgeinsets和imageedgeinsets 属性

customizebtn.h

 

#import 

typedef ibinspectable ns_enum(nsuinteger, customizebtnimageposition) {
    customizebtnimagepositiontop, // image在上,label在下
    customizebtnimagepositionleft, // image在左,label在右
    customizebtnimagepositionbottom, // image在下,label在上
    customizebtnimagepositionright // image在右,label在左
};

ib_designable
@interface customizebtn : uibutton
@property(nonatomic,unsafe_unretained)ibinspectable nsuinteger imageposition;
@property(nonatomic,unsafe_unretained)ibinspectable nsuinteger space;

@end

 

customizebtn.m

 

#import "customizebtn.h"

@implementation customizebtn

- (void)drawrect:(cgrect)rect {
    [self btnimageposition:self.imageposition imagetitlespace:self.space];
}

-(void)setimageposition:(nsuinteger)imageposition {
    _imageposition = imageposition;
    [self btnimageposition:imageposition imagetitlespace:self.space];
}

-(void)setspace:(nsuinteger)space {
    _space = space;
    [self btnimageposition:self.imageposition imagetitlespace:space];
}

- (void)btnimageposition:(customizebtnimageposition)style
                        imagetitlespace:(cgfloat)space {
    
    cgfloat imagewith = self.imageview.frame.size.width;
    cgfloat imageheight = self.imageview.frame.size.height;
    
    cgfloat labelwidth = 0.0;
    cgfloat labelheight = 0.0;
    if ([uidevice currentdevice].systemversion.floatvalue >= 8.0) {
        // 由于ios8中titlelabel的size为0,用下面的这种设置
        labelwidth = self.titlelabel.intrinsiccontentsize.width;
        labelheight = self.titlelabel.intrinsiccontentsize.height;
    } else {
        labelwidth = self.titlelabel.frame.size.width;
        labelheight = self.titlelabel.frame.size.height;
    }
    
    uiedgeinsets imageedgeinsets = uiedgeinsetszero;
    uiedgeinsets labeledgeinsets = uiedgeinsetszero;
    
    switch (style) {
        case customizebtnimagepositiontop:
        {
            imageedgeinsets = uiedgeinsetsmake(-labelheight-space/2.0, 0, 0, -labelwidth);
            labeledgeinsets = uiedgeinsetsmake(0, -imagewith, -imageheight-space/2.0, 0);
        }
            break;
        case customizebtnimagepositionleft:
        {
            imageedgeinsets = uiedgeinsetsmake(0, -space/2.0, 0, space/2.0);
            labeledgeinsets = uiedgeinsetsmake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case customizebtnimagepositionbottom:
        {
            imageedgeinsets = uiedgeinsetsmake(0, 0, -labelheight-space/2.0, -labelwidth);
            labeledgeinsets = uiedgeinsetsmake(-imageheight-space/2.0, -imagewith, 0, 0);
        }
            break;
        case customizebtnimagepositionright:
        {
            imageedgeinsets = uiedgeinsetsmake(0, labelwidth+space/2.0, 0, -labelwidth-space/2.0);
            labeledgeinsets = uiedgeinsetsmake(0, -imagewith-space/2.0, 0, imagewith+space/2.0);
        }
            break;
        default:
            break;
    }
    
    self.titleedgeinsets = labeledgeinsets;
    self.imageedgeinsets = imageedgeinsets;
}

@end