2010-12-01 41 views
0

我今天安装了4.2 iphone sdk,并用4.2运行我的项目(4.0中编写)。我注意到,工具栏项目放在工具栏上,无论是在iPad,iPhone模拟器。除此之外,还有谁遇到过这个问题吗?下面是代码:iphone sdk 4.2:UIBarButtonItem的UBSoolbar错位

@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 

下面是它的输出:

output http://dl.dropbox.com/u/13741164/toolbar.jpg

正如你看到的,详细信息按钮出帧由右侧。较小的TOOLBAR_​​ITEM_WIDTH,它在框架内越多。所以我想在工具栏项目的计算中存在一个错误,因为它在4.0 sdk中很好用,对吧?谢谢你的帮助。

+0

我找到了解决办法。在4.2中,即使后面调用了-setFrame,uitoolbar也不应该用-init初始化,相反,-initWithFrame应该与一个足以包含所有按钮的虚拟框架一起使用。 – 2010-12-02 09:52:17

回答

2

以下调用解决了该问题对我来说:

[toolbar sizeToFit];