Ios – UIButton with fixed size custom image

iosipadiphoneobjective cuibutton

I have a custom UIButton as follows:

 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];

The real image that I have is actually sized at 10×10, centered and I want it to stay that way and not scaled to the button size. How do I do that?

REVISED CODE:

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];

which is still scaling

Best Answer

for this i think you need to make use of the image insets property UIButton Class Reference