UIButton的四种常用类型

做项目Demo的时候偶尔想到这个按钮比较常用 , 就写出来了 , 可以写成工具类方法给项目使用;

图例

image.png


#import "RecommendViewCtrl.h"

@interface RecommendViewCtrl ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>

@end

@implementation RecommendViewCtrl

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.view.backgroundColor = [UIColor whiteColor];
        self.tabBarItem.title = @"推荐";
        self.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/like@2x.png"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/like_selected@2x.png"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.backgroundColor = [UIColor lightGrayColor];
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 5, self.view.bounds.size.height);
    scrollView.delegate = self;
    NSArray *colorArray = @[[UIColor redColor],[UIColor blueColor],[UIColor yellowColor],[UIColor lightGrayColor],[UIColor grayColor]];
    
    for (int i = 0; i < 5; i++) {
        [scrollView addSubview:({
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(scrollView.bounds.size.width * i, 0, scrollView.bounds.size.width, scrollView.bounds.size.height)];
            
            // 1.左图右文:
            [view addSubview:({
                UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
                [button1 setFrame:CGRectMake(100,50, 100, 100)];
                button1.backgroundColor = [UIColor orangeColor];
                [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
                
                [button1 setTitle:@"左图右文" forState:UIControlStateNormal];
                [button1.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
                [button1 setImage:[UIImage imageNamed:@"icon.bundle/like_selected@2x.png"] forState:UIControlStateNormal];
                [button1 setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
                button1;
                
            })];
            
            // 2.左文右图:
            [view addSubview:({
                UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
                [button2 setFrame:CGRectMake(100, 150, 100, 100)];
                button2.backgroundColor = [UIColor purpleColor];
                
                [button2 setTitle:@"左文右图" forState:UIControlStateNormal];
                [button2.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
                [button2 setImage:[UIImage imageNamed:@"icon.bundle/like_selected@2x.png"] forState:UIControlStateNormal];
                // 文字偏移量:
                [button2 setTitleEdgeInsets:UIEdgeInsetsMake(0, - button2.imageView.width - 10, 0, button2.imageView.width)];
                // 图片偏移量:
                [button2 setImageEdgeInsets:UIEdgeInsetsMake(0, button2.titleLabel.width, 0, - button2.titleLabel.width)];
                button2;
            })];

            // 3.上图下文:
            [view addSubview:({
                
                UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
                [button3 setFrame:CGRectMake(100, 250, 100, 100)];
                button3.backgroundColor = [UIColor lightGrayColor];
                [button3 setTitle:@"上图下文" forState:UIControlStateNormal];
                [button3.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
                [button3 setImage:[UIImage imageNamed:@"icon.bundle/like_selected@2x.png"] forState:UIControlStateNormal];

                // 文字偏移量:往下 , 往左移动:
                [button3 setTitleEdgeInsets:UIEdgeInsetsMake(button3.imageView.height + 10, - button3.imageView.width, 0, 0)];
                // 图片偏移量:往上 , 往右移动:
                [button3 setImageEdgeInsets:UIEdgeInsetsMake(0, button3.titleLabel.width / 2, button3.titleLabel.height + 10.0, - button3.titleLabel.width / 2)];
                button3.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
                
                button3;
                
            })];
            
            // 4.上文下图:
            [view addSubview:({
                
                UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
                [button4 setFrame:CGRectMake(100, 350, 100, 100)];
                button4.backgroundColor = [UIColor greenColor];
                [button4 setTitle:@"上图下文" forState:UIControlStateNormal];
                [button4.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
                [button4 setImage:[UIImage imageNamed:@"icon.bundle/like_selected@2x.png"] forState:UIControlStateNormal];
                
                // 文字偏移量:往上 , 往左移动:
                [button4 setTitleEdgeInsets:UIEdgeInsetsMake(- button4.imageView.height + 10, - button4.imageView.width, 0, 0)];
                // 图片偏移量:往下 , 往右移动:
                [button4 setImageEdgeInsets:UIEdgeInsetsMake(button4.titleLabel.height + 10, button4.titleLabel.width / 2, - button4.titleLabel.height - 10, - button4.titleLabel.width / 2)];
                button4;
                
            })];
            
            view.backgroundColor = [colorArray objectAtIndex:i];
            view;
        })];
        
    }
    scrollView.pagingEnabled = YES;
    [self.view addSubview:scrollView];
}

- (void)buttonClicked:(UIButton *)button
{
    NSLog(@"buttonClicked");
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"scrollViewDidScroll - %@",@(scrollView.contentOffset.x));
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"BeginDragging");
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"EndDragging");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
}

@end

愿编程让这个世界更美好

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

推荐阅读更多精彩内容

  • 徐鹤宁老师主讲:如何快速成为行业第一如何一对一谈判说服!如何倍增时间、倍增自己年轻大成功的秘密!如何问话式销售系统...
    8913be35eba5阅读 171评论 0 1
  • 90后现在已经走向社会,并在各种工作岗位中有着不可替代的作用,那怎样管理、了解、领导90后也是一门学问,通过...
    ASEN007阅读 562评论 0 1
  • 昨天通过多线程实现方案之 -- NSThread说了关于 NSThread 多线程的一些知识点和用法, 其实之...
    devZhang阅读 2,726评论 24 50