[IOS笔记]改变UIButton中图片和文字的位置 xib与故事版的差别


//  UEButton.h
//  UIButtonInner
//
//  Created by cdj on 17/9/9.
//  Copyright © 2017年 ue. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UEButton : UIButton

@end

//  UEButton.m
//  UIButtonInner
//
//  Created by cdj on 17/9/9.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "UEButton.h"

@implementation UEButton

/*
重写BUttion类的两个方法,改变图片和文字的位置
*/
//方式一
//-(CGRect)titleRectForContentRect:(CGRect)contentRect{
//    return CGRectMake(0, 0, 100, 70);
//}
//
//-(CGRect)imageRectForContentRect:(CGRect)contentRect{
//
//    return CGRectMake(100, 0, 70, 70);
//}

//方式二
-(void)layoutSubviews{
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(0, 0, 100, 70);
    self.imageView.frame = CGRectMake(100, 0, 70, 70);
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //按钮文字剧中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        //改变图片的内容模式
        self.imageView.contentMode = UIViewContentModeCenter;
    }
    return self;
}

@end

//  ViewController.m
//  UIButtonInner
//
//  Created by cdj on 17/9/9.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "ViewController.h"
#import "UEButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UEButton *button = [UEButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(50, 50, 170, 70);
    button.backgroundColor = [UIColor purpleColor];
    
    //添加图标:默认图标在左,文字在右
    [button setTitle:@"normal" forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"miniplayer_btn_playlist_normal"] forState:UIControlStateNormal];
    
    //该变位置
    button.imageView.backgroundColor = [UIColor yellowColor];
    button.titleLabel.backgroundColor = [UIColor blueColor];
    
    [self.view addSubview:button];
}

@end



































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值