首页 > 代码库 > IOS UIButton常用属性

IOS UIButton常用属性

 

 

   
        //1.添加按钮
        UIButton *nameView=[UIButton buttonWithType:UIButtonTypeCustom];
        //nameView.backgroundColor=[UIColor redColor];
        [nameView setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        
        [nameView setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
        
        //设置按钮内部的左边箭头图片
        [nameView setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        [nameView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
//        //设置按钮的内容左对齐
//        nameView.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
        // 设置按钮的内容左对齐
        nameView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        //设置按钮的内边距
        nameView.titleEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0);
        nameView.contentEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0);
                [nameView addTarget:self action:@selector(nameViewClick) forControlEvents:UIControlEventTouchUpInside];
        
        //设置按钮内部的imageView的内容模型为居中
        nameView.imageView.contentMode=UIViewContentModeCenter;
        
        //超出边框的内容不需要裁剪
        nameView.imageView.clipsToBounds=NO;
        
        [self.contentView addSubview:nameView];
        self.nameView=nameView;
        

 

IOS UIButton常用属性