button上的图片和文字偏移量排版

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#pragma mark - 滚动视图
- (void)creatView{
    
    NSArray *xarr =@[@"title_tab_essence_normal_night",@"title_tab_newissue_normal_night",@"title_tab_suiji_normal_night"];
    NSArray *farr =@[@"title_tab_essence_normal",@"title_tab_newissue_normal",@"title_tab_suiji_normal"];
    NSArray *lab = @[@"精华",@"最新",@"穿越"];
    for (int i = 0; i<3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        //设置button上的正常情况下显示的图片
        [btn setImage:[[UIImage imageNamed:farr[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] forState:UIControlStateNormal];
        //设置button上的被点击后显示的图片
        [btn setImage:[[UIImage imageNamed:xarr[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] forState:UIControlStateSelected];
        //设置button上图片的偏移量
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 9.0, 25.0, 9.0) ];
        //设置button大小
        btn.frame = CGRectMake(90+i*50, 5, 50, 50);
        //设置button方法的实现
        [btn addTarget:self action:@selector(Click:)
      forControlEvents:UIControlEventTouchUpInside];
        //设置button上的正常情况下显示的字体的颜色
        btn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        [btn setTitle:lab[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        //设置button上的被点击后的显示的字体颜色
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        //设置button上字体的偏移量
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(10.0,-40.0 , 0.0, -8.0)];
        //设置tag值
        btn.tag = 100+i;
        //运行程序默认被选中的button
        if (btn.tag == 100) {
            btn.selected = YES;
        }
        //粘贴到导航条上
        [self.navigationController.navigationBar addSubview:btn];
    }
}
#pragma mark - button方法实现
- (void)Click:(UIButton *)btn{
    //设置被选中
    btn.selected = YES;
    //遍历navigationBar获取subviews子视图
    for (UIView *vie in self.navigationController.navigationBar.subviews) {
        if ([vie isKindOfClass:[UIButton class]]) {
            //没有被选中的button设置为未被选中状态
            if (vie.tag !=btn.tag) {
                ((UIButton *)vie).selected = NO;
            }
        }
    }
}