我是靠谱客的博主 霸气火,这篇文章主要介绍自定义UIButton,现在分享给大家,希望可以做个参考。

先建一个页面继承自UIButton




//.h文件

#import <UIKit/UIKit.h>


@interface MyButton : UIButton


+ (UIButton *)buttonWithTitle:(NSString *)title frame:(CGRect) frame target:(id)target action:(SEL)action backgroundImage:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage;


@end




//.m文件

#import "MyButton.h"


@implementation MyButton


+ (UIButton *)buttonWithTitle:(NSString *)title frame:(CGRect) frame target:(id)target action:(SEL)action backgroundImage:(NSString *)backgroundImage selectedImage:(NSString *)selectedImage {

    //  创建一个按钮

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    //   设置按钮背景颜色

    //    button.backgroundColor = [UIColor colorWithRed:0.601 green:0.596 blue:0.906 alpha:1.000];

    //  设置按钮大小

    button.frame = frame;

    //    设置背景图片

    [button setBackgroundImage:[UIImage imageNamed:backgroundImage] forState:UIControlStateNormal];

    //   设置背景选择图片

    [button setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateHighlighted];

    //   设置按钮的标题

    [button setTitle:title forState:UIControlStateNormal];

    //   设置按钮的点击事件

    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    

    return button;

}



最后

以上就是霸气火最近收集整理的关于自定义UIButton的全部内容,更多相关自定义UIButton内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部