iOS-UIButton的基本使用

一、UIButton的基本设置

//初始化按钮  
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];  
//UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 120, 50)];  
  
//按钮类型  
typedef NS_ENUM(NSInteger, UIButtonType) {  
    UIButtonTypeCustom = 0,                         //无类型,需要自定义  
    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  //系统类型  
    //以下3种样式,iOS7以前不一样,扁平化以后都一样  
    UIButtonTypeDetailDisclosure, //详细描述样式,圆圈中间加个i  
    UIButtonTypeInfoLight,       //浅色的详细描述样式  
    UIButtonTypeInfoDark,       //深色的详细描述样式  
      
    UIButtonTypeContactAdd,    //圆圈中间有个加号  
      
    UIButtonTypePlain API_AVAILABLE(tvos(11.0)) __IOS_PROHIBITED __WATCHOS_PROHIBITED,   
      
    UIButtonTypeRoundedRect = UIButtonTypeSystem  //iOS7以后圆角按钮被 UIButtonTypeSystem 取代 两者等同  
};  
  
//设置位置尺寸  
button.frame = CGRectMake(100, 100, 120, 50);  
  
//设置按钮的背景颜色  
button.backgroundColor = [UIColor cyanColor];  
  
//设置文字  
[button setTitle:@"按钮" forState:UIControlStateNormal];  
//设置文字颜色  
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
//设置字体大小  
button.titleLabel.font = [UIFont systemFontOfSize:25];  
//设置文字的阴影颜色  
[button setTitleShadowColor:[UIColor yellowColor] forState:UIControlStateNormal];  
  
//设置背景图片  
[button setImage:[UIImage imageNamed:@"bottom_normal"] forState:UIControlStateNormal];  
  
//forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现  
 typedef NS_OPTIONS(NSUInteger, UIControlState) {  
    UIControlStateNormal       = 0,       //常规状态  
    UIControlStateHighlighted  = 1 << 0,  //高亮状态  
    UIControlStateDisabled     = 1 << 1,  //禁用的状态  
    UIControlStateSelected     = 1 << 2,  //选中状态  
    UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, //聚焦状态  
 }; 

二、获取Button相关参数

@property(nonatomic)    UIEdgeInsets  contentEdgeInsets; //内容内边距 
@property(nonatomic)    UIEdgeInsets  titleEdgeInsets;   //标题内边距
@property(nonatomic)    UIEdgeInsets  imageEdgeInsets;  //图片内边距
@property(nonatomic)    BOOL   reversesTitleShadowWhenHighlighted;//标题的阴影改变时,按钮是否高亮显示.默认为NO 
@property(nonatomic)    BOOL   adjustsImageWhenHighlighted;//按钮高亮的情况下,图像的颜色是否要加深一点.默认是YES 
@property(nonatomic)    BOOL   adjustsImageWhenDisabled; //按钮禁用的情况下,图像的颜色是否要加深一点.默认是YES
@property(nonatomic)    BOOL   showsTouchWhenHighlighted;//按下按钮是否会发光,默认是NO

//返回button 某个状态下的标题
- (nullable NSString *)titleForState:(UIControlState)state;
//返回button 某个状态下的标题颜色         
- (nullable UIColor *)titleColorForState:(UIControlState)state;
//返回button 某个状态下的阴影标题颜色
- (nullable UIColor *)titleShadowColorForState:(UIControlState)state;
//返回button 某个状态下的图片
- (nullable UIImage *)imageForState:(UIControlState)state;
//返回button 某个状态下的背景图片
- (nullable UIImage *)backgroundImageForState:(UIControlState)state;

@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;             // 当前标题
@property(nonatomic,readonly,strong) UIColor  *currentTitleColor;                  // 当前标题颜色
@property(nullable, nonatomic,readonly,strong) UIColor  *currentTitleShadowColor;  // 当前标题阴影颜色
@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;             // 当前图片
@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage;   // 当前背景图片
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle;  // 当前富文本标题

@property(nullable, nonatomic,readonly,strong) UILabel     *titleLabel; // 当前标题UILabel
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView; // 当前图片UIImageView

三、事件监听

[button addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
-(void)butClick:(UIButton *)button
{
    
}

//事件状态
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
 UIControlEventTouchDown,        //单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候
 UIControlEventTouchDownRepeat,  //多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候
 UIControlEventTouchDragInside,  //当一次触摸在控件窗口内拖动时
 UIControlEventTouchDragOutside, //当一次触摸在控件窗口之外拖动时
 UIControlEventTouchDragEnter,   //当一次触摸从控件窗口之外拖动到内部时
 UIControlEventTouchDragExit,    //当一次触摸从控件窗口内部拖动到外部时
 UIControlEventTouchUpInside,    //所有在控件之内触摸抬起事件
 UIControlEventTouchUpOutside,   //所有在控件之外触摸抬起事件
 UIControlEventTouchCancel,      //所有触摸取消事件
 
 UIControlEventValueChanged,     //当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。
 
 UIControlEventEditingDidBegin,      // 当文本控件中开始编辑时发送通知
 UIControlEventEditingChanged,       // 当文本控件中的文本被改变时发送通知
 UIControlEventEditingDidEnd,        // 当文本控件中编辑结束时发送通知
 UIControlEventEditingDidEndOnExit,  // 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知
 
 UIControlEventAllTouchEvents,       // 通知所有触摸事件
 UIControlEventAllEditingEvents,     // 通知所有关于文本编辑的事件
 UIControlEventApplicationReserved,  // range available for application use
 UIControlEventSystemReserved,       // range reserved for internal framework use
 
 UIControlEventAllEvents,            // 通知所有事件
};


你可能感兴趣的:(iOS)