0

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 non-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.

KingofBliss
  • 15,055
  • 6
  • 50
  • 72
ozan k
  • 485
  • 5
  • 18
  • I found the solution. In 4.2, uitoolbar should not be initialized with -init even if -setFrame is called later, rather -initWithFrame should be used with a dummy frame which is big enough to contain all the buttons. – ozan k Dec 02 '10 at 09:52

1 Answers1

2

The following call fixed the issue for me:

[toolbar sizeToFit];
Cosmin
  • 21,216
  • 5
  • 45
  • 60
Prakash
  • 21
  • 2