使用继承创建自定义按钮,图片在上,标题在下

//
//  ZXSTopImageTextbutton.h
//  ZXSOCAPP
//
//  Created by bi xu on 2018/6/1.
//  Copyright © 2018年 CoderZXS. All rights reserved.
//  使用继承创建自定义按钮,图片在上,标题在下

#import <UIKit/UIKit.h>

@interface ZXSTopImageTextbutton : UIButton

- (instancetype)zxs_initWithFrame:(CGRect)frame image:(UIImage *)image target:(id)target action:(SEL)action title:(NSString *)title;

@end


-------------------------------------------------------------------

//
//  ZXSTopImageTextbutton.m
//  ZXSOCAPP
//
//  Created by bi xu on 2018/6/1.
//  Copyright © 2018年 CoderZXS. All rights reserved.


#import "ZXSTopImageTextbutton.h"

@implementation ZXSTopImageTextbutton

#pragma mark - 系统方法
- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    CGFloat buttonWidth = self.frame.size.width;
    CGFloat buttonHeight = self.frame.size.height;
    self.imageView.frame = CGRectMake((buttonWidth * 0.2), 0, (buttonWidth * 0.6), (buttonWidth * 0.6));
    
    CGFloat titleLabelY = (CGRectGetMaxY(self.imageView.frame) + buttonHeight * 0.2);
    self.titleLabel.frame = CGRectMake(0, titleLabelY, buttonWidth, (buttonHeight - titleLabelY));
}



#pragma mark - 自定义方法
- (instancetype)zxs_initWithFrame:(CGRect)frame image:(UIImage *)image target:(id)target action:(SEL)action title:(NSString *)title {
    if (self == [super initWithFrame:frame]) {
        
        [self setImage:image forState:UIControlStateNormal];
        [self setTitle:title forState:UIControlStateNormal];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return self;
}


@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容