【发布时间】:2024-01-18 11:55:01
【问题描述】:
我有一个自定义 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];
仍在缩放
【问题讨论】:
标签: iphone objective-c ios ipad uibutton