iphone sdk 4.2: UIBarButtonItem' s are misplaced over UIToolbar
I have installed 4.2 iphone sdk today and ran my project (which is written in 4.0) with 4.2. I noticed that toolbar items are misplaced over toolbar, both in ipad, iphone simulators. Is there anyone else other than me who has encountered with this? Here is the code:
@define TOOLBAR_HEIGHT 34
@define TOOLBAR_ITEM_WIDTH 90
// extends UIView
@implementation MapViewControllerContainer
- (void) setFrame:(CGRect)frame
{
[super setFrame:frame];
if (self.subviews.count == 2)
{
((UIView *)[self.subviews objectAtIndex:0]).frame = CGRectMake(0, 0, frame.size.width, TOOLBAR_HEIGHT);
((UIView *)[self.subviews objectAtIndex:1]).frame = CGRectMake(0, TOOLBAR_HEIGHT, frame.size.width,
frame.size.height - TOOLBAR_HEIGHT);
}
}
@end
// extends UIViewController
@implementation MapViewController
- (void) loadView
{
self.view = [[MapViewControllerContainer alloc] init];
[self.view release];
UIToolbar * toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlack;
UIBarButtonItem * flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
detailsButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Details",@"")
style:UIBarButtonItemStyleBordered target:self
action:@selector(detailsPressed)];
detailsButton.width = TOOLBAR_ITEM_WIDTH;
detailsButton.enabled = NO;
toolbar.items = [NSArray arrayWithObjects: flexibleItem, detailsButton, nil];
[flexibleItem release];
[detailsButton release];
[self.view addSubview:toolbar];
[toolbar release];
// More no开发者_高级运维n-related code here
}
@end
Here is its output:
output http://dl.dropbox.com/u/13741164/toolbar.jpg
As you see, details button is out of frame by the right side. The smaller TOOLBAR_ITEM_WIDTH, the more it is inside the frame. So I suppose there is a bug in the calculation of toolbar items, because it works wonderfully in 4.0 sdk, right? Thanks for your help.
The following call fixed the issue for me:
[toolbar sizeToFit];
精彩评论