UIButton具有固定大小的自定义图像

adi*_*dit 5 iphone objective-c uibutton ipad ios

我有一个自定义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];
Run Code Online (Sandbox Code Playgroud)

我拥有的真实图像实际上是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];
Run Code Online (Sandbox Code Playgroud)

这仍然是缩放

bor*_*den 2

为此,我认为您需要使用图像插入属性UIButton 类参考