1
votes

I try with a normal big size icon, not resized at all, and the UIBarButtonItemStyleBordered works:

UIBarButtonItem *compassButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"compass.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(compassButtonPressed:)];
compassButtonItem.enabled = YES;
self.navigationItem.rightBarButtonItem = compassButtonItem;

UIBarButtonItem initWithImage

Then I try to resize the image to 20x20, using a UIButton and a CGRectMake, the border doesn't work:

UIButton *compassButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIImage *compassImage = [UIImage imageNamed:@"compass.png"];
[compassButton addTarget:self action:@selector(compassButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[compassButton setImage:compassImage forState:UIControlStateNormal];
[compassButton setImage:compassImage forState:UIControlStateSelected];
UIBarButtonItem *compassButtonItem = [[UIBarButtonItem alloc] initWithCustomView:compassButton];
compassButtonItem.style = UIBarButtonItemStyleBordered;
compassButtonItem.enabled = YES;
self.navigationItem.rightBarButtonItem = compassButtonItem;

UIButton setImage

Any ideas how to use the UIBarButtonItemStyleBordered with an UIImage resized?.

1
You didnt use the UIBarButtonItem for the compass you used UIButton.deleted_user

1 Answers

4
votes

How about:

UIImage *image = [UIImage imageNamed:@"compass.png"];
CGSize newSize = CGSizeMake(20.0f, 20.0f); 
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *compassButtonItem = [[UIBarButtonItem alloc] initWithImage:thumbnail style:UIBarButtonItemStyleBordered target:self action:@selector(refreshSection)];  
self.navigationItem.rightBarButtonItem = compassButtonItem;