后退按钮在自定义导航栏iPad上显示一半

时间:2011-11-14 13:25:35

标签: ipad

自定义导航栏上的后退按钮在iPad上显示一半,在iPhone上显示正确。 app仅支持纵向模式。

enter image description here

源代码,

  UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
if (isPad) {
    backButton.frame = CGRectMake(0, 0, 120, 70);
    [backButton setBackgroundImage:[UIImage imageNamed:@"BackButtonUnpressed-iPad_120X70.png"] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[UIImage imageNamed:@"BackButtonPressed-iPad_120X70.png"] forState:UIControlStateHighlighted];
}else{
    backButton.frame = CGRectMake(0, 0, 60, 35);
    [backButton setBackgroundImage:[UIImage imageNamed:@"BackButtonUnpressed.png"] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[UIImage imageNamed:@"BackButtonPressed.png"] forState:UIControlStateHighlighted];
}

[backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release]; buttonItem = nil;

请帮忙。

2 个答案:

答案 0 :(得分:0)

啊,我刚刚意识到你试图通过一个初始化器将一个UIButton控件猛烈撞击到一个UIBarButtonItem,这个初始化器期望一个基本的UIView而不是一个控件。

不要那样做!

为什么不通过以下方式创建条形按钮项目:

UIBarButtonItem * buttonItem = 
    [[UIBarButtonItem alloc] initWithTitle: @"Back" 
                                     style: UIBarButtonItemStyleDone 
                                    target: self 
                                    action: @selector(backButtonPressed:);

让O.S.处理适当的突出显示(如果有的话)。

答案 1 :(得分:0)

我解决了这个问题,在此处发布了详细信息,以便对其他人有所帮助

1] created a UIButton, with custom image.
2] created a UIView, which will fit properly on UINavigationbar.
3] added UIButton on UIView.
4] created UIBarButtonItem with custom view.

这是代码,

    if (isPad) {
        UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 200, 200)];
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 90, 120, 70);
        [backButton setBackgroundImage:[UIImage imageNamed:@"BackButtonUnpressed-iPad_120X70.png"] forState:UIControlStateNormal];
        [backButton setBackgroundImage:[UIImage imageNamed:@"BackButtonPressed-iPad_120X70.png"] forState:UIControlStateHighlighted];
        [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [customView addSubview:backButton];

        UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
        self.navigationItem.leftBarButtonItem = buttonItem;
        [customView release];  customView = nil;
        [buttonItem release]; buttonItem = nil;
    }else{
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 0, 60, 35);
        [backButton setBackgroundImage:[UIImage imageNamed:@"BackButtonUnpressed.png"] forState:UIControlStateNormal];
        [backButton setBackgroundImage:[UIImage imageNamed:@"BackButtonPressed.png"] forState:UIControlStateHighlighted];
        [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = buttonItem;
        [buttonItem release]; 
        buttonItem = nil;
    }