UIButton默认image在左,title在又,如果需要设置图片文字之间的距离,需要设置edgeInsets,例如:
- (UIButton *)moreMsgButton {
if (!_moreMsgButton) {
_moreMsgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_moreMsgButton setTitle:@"有新消息" forState:UIControlStateNormal];
_moreMsgButton.titleLabel.font = [UIFont systemFontOfSize:13.0];
[_moreMsgButton setImage:[UIImage imageNamed:@"popWindow_arrow_default"] forState:UIControlStateNormal];
_moreMsgButton.layer.cornerRadius = 17.5;
[self.view addSubview:_moreMsgButton];
_moreMsgButton.frame = CGRectMake(CGRectGetWidth(self.view.frame)-100+17.5, 80, 100, 35);
//图片相对于button的偏移量
[_moreMsgButton setImageEdgeInsets:UIEdgeInsetsMake(13, 10, 13, _moreMsgButton.frame.size.width-10-11)];
//文字相对于图片的偏移量
[_moreMsgButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
}
return _moreMsgButton;
}
设置图片距离左10,上下缩进13。文字距离图片左侧是10。