1

我尝试使用普通的大尺寸图标,根本没有调整大小,并且 UIBarButtonItemStyleBordered 有效:

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

然后我尝试使用 UIButton 和 CGRectMake 将图像大小调整为 20x20,边框不起作用:

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

任何想法如何使用 UIBarButtonItemStyleBordered 和 UIImage 调整大小?

4

1 回答 1

4

怎么样:

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;  
于 2012-10-22T20:19:09.880 回答