我有一个自定义的 UIButton,如下所示:
UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom];
[action setSize:CGSizeMake(30, 30)];
[action setBackgroundImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal];
[action setContentMode:UIViewContentModeCenter];
[action addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
我拥有的真实图像实际上大小为 10x10,居中,我希望它保持这种状态,而不是缩放到按钮大小。我该怎么做?
修改后的代码:
UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom];
[action setSize:CGSizeMake(44, 44)];
[action setImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal];
[action addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
if (IS_IPAD){
action.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
action.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}
actionButton_ = [[UIBarButtonItem alloc] initWithCustomView:action];
仍在扩展
最佳答案
为此,我认为您需要使用图像插入属性 UIButton Class Reference
关于iphone - 具有固定大小自定义图像的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9711437/