iphonesdk4.2:uibarbuttonim';在UIToolbar上放错了s

iphonesdk4.2:uibarbuttonim';在UIToolbar上放错了s,iphone,objective-c,ios-4.2,Iphone,Objective C,Ios 4.2,我今天已经安装了4.2iPhoneSDK,并用4.2运行了我的项目(用4.0编写)。我注意到,无论是在ipad还是iphone模拟器中,工具栏项目都放错了位置。除了我,还有其他人遇到过这个问题吗?代码如下: @define TOOLBAR_HEIGHT 34 @define TOOLBAR_ITEM_WIDTH 90 // extends UIView @implementation MapViewControllerContainer - (void) setFrame:(CGRect)fr

我今天已经安装了4.2iPhoneSDK,并用4.2运行了我的项目(用4.0编写)。我注意到,无论是在ipad还是iphone模拟器中,工具栏项目都放错了位置。除了我,还有其他人遇到过这个问题吗?代码如下:

@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
以下是它的输出:


如您所见,详细信息按钮位于右侧的框架之外。工具栏\u项目\u宽度越小,它在框架内的位置就越多。所以我想工具栏项的计算中有一个bug,因为它在4.0SDK中运行得非常好,对吧?谢谢您的帮助。

下面的电话为我解决了这个问题:

[toolbar sizeToFit];

我找到了解决办法。在4.2中,uitoolbar不应使用-init初始化,即使稍后调用了-setFrame,而是应将-initWithFrame与一个足够大以包含所有按钮的虚拟框架一起使用。