setImage方法设置背景
1 self.btnDateline=[[UIButton alloc]init]; 2 self.btnDateline.tag=tagBtnDateLine; 3 self.btnDateline.titleLabel.font=[UIFont systemFontOfSize:18]; 4 [self.btnDateline setTitle:@"请选择" forState:UIControlStateNormal]; 5 //[self.btnDateline setBackgroundImage:_imgPicker forState:UIControlStateNormal]; 6 [self.btnDateline setImage:_imgPicker forState:UIControlStateNormal]; 7 //[self.btnDateline setBackgroundColor:[UIColor colorWithPatternImage:_imgPicker]]; 8 self.btnDateline.userInteractionEnabled=YES; 9 [self.scrollview addSubview:self.btnDateline]; 10 [self.btnDateline addTarget:self action:@selector(btnTagged:) forControlEvents:UIControlEventTouchUpInside];
此时图片根据button的尺寸自动进行了缩放,但遮盖了title
setBackgroundColor方法
self.btnDateline=[[UIButton alloc]init]; self.btnDateline.tag=tagBtnDateLine; self.btnDateline.titleLabel.font=[UIFont systemFontOfSize:18]; [self.btnDateline setTitle:@"请选择" forState:UIControlStateNormal]; //[self.btnDateline setBackgroundImage:_imgPicker forState:UIControlStateNormal]; //[self.btnDateline setImage:_imgPicker forState:UIControlStateNormal]; [self.btnDateline setBackgroundColor:[UIColor colorWithPatternImage:_imgPicker]]; self.btnDateline.userInteractionEnabled=YES; [self.scrollview addSubview:self.btnDateline]; [self.btnDateline addTarget:self action:@selector(btnTagged:) forControlEvents:UIControlEventTouchUpInside];
此时图片作为背景色,未遮盖了title,但不能根据button的尺寸自动进行了缩放
setBackgroundImage
self.btnDateline=[[UIButton alloc]init]; self.btnDateline.tag=tagBtnDateLine; self.btnDateline.titleLabel.font=[UIFont systemFontOfSize:18]; [self.btnDateline setTitle:@"请选择" forState:UIControlStateNormal]; [self.btnDateline setBackgroundImage:_imgPicker forState:UIControlStateNormal]; //[self.btnDateline setImage:_imgPicker forState:UIControlStateNormal]; //[self.btnDateline setBackgroundColor:[UIColor colorWithPatternImage:_imgPicker]]; self.btnDateline.userInteractionEnabled=YES; [self.scrollview addSubview:self.btnDateline]; [self.btnDateline addTarget:self action:@selector(btnTagged:) forControlEvents:UIControlEventTouchUpInside];
此时图片未遮盖了title,且也能根据button的尺寸自动进行了缩放