Iphone 更改UIBarButtonItem UIButton';s形象

Iphone 更改UIBarButtonItem UIButton';s形象,iphone,objective-c,ios,ipad,uinavigationbar,Iphone,Objective C,Ios,Ipad,Uinavigationbar,我有一个UIBarButtonItem,它有一个自定义的UIbutton视图和一个图像,我想为不同的方向创建一个不同的UIImage,下面是我的代码: UIButton * backButton = (UIButton *) ((UIBarButtonItem *) self.navBar_.topItem.leftBarButtonItem).customView; if (UIInterfaceOrientationIsLandscape([[UIApplication s

我有一个UIBarButtonItem,它有一个自定义的UIbutton视图和一个图像,我想为不同的方向创建一个不同的UIImage,下面是我的代码:

 UIButton * backButton = (UIButton *) ((UIBarButtonItem *) self.navBar_.topItem.leftBarButtonItem).customView;
        if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && IS_IPAD) {        
            [backButton setBackgroundImage:[UIImage imageNamed:@"back-landscape.png"] forState:UIControlStateNormal];  
            [backButton setFrame: CGRectMake(0, 0, 46, 35)]; 
        } else {
            [backButton setBackgroundImage:[UIImage imageNamed:@"back-reading.png"] forState:UIControlStateNormal];  
            [backButton setFrame: CGRectMake(0, 0, 23, 19)]; 
        }

但是,这不会更改UIButton图像,为什么会这样?

您可以使用新的代理类和+apperence方法检查文档,但仅适用于iOS5。对于早期的支持,您可以在视图旋转时更新图像,方法是视图控制器-willRotateToInterfaceOrientation。
Ciao

您可以使用新的代理类和+appreance方法检查文档,但仅适用于iOS5。对于早期的支持,您可以在视图旋转时更新图像,方法是视图控制器-willRotateToInterfaceOrientation。 再见

试试这个:-

UIButton * backButton ;
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && IS_IPAD) 
{ 
    backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 66.0f, 36.0f)];       
    [backButton setImage:[UIImage imageNamed:@"back-landscape.png"] forState:UIControlStateNormal];  

} 
else 
{
    backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 23, 19)];     
    [backButton setImage:[UIImage imageNamed:@"back-reading.png"] forState:UIControlStateNormal];  
    [backButton setFrame: CGRectMake(0, 0, 23, 19)]; 
}

[backButton addTarget:self action:@selector(goToHome) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *HomeButton = [[UIBarButtonItem alloc] initWithCustomView: backButton];
[self.navigationItem setLeftBarButtonItem:HomeButton];
试试这个:-

UIButton * backButton ;
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) && IS_IPAD) 
{ 
    backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 66.0f, 36.0f)];       
    [backButton setImage:[UIImage imageNamed:@"back-landscape.png"] forState:UIControlStateNormal];  

} 
else 
{
    backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 23, 19)];     
    [backButton setImage:[UIImage imageNamed:@"back-reading.png"] forState:UIControlStateNormal];  
    [backButton setFrame: CGRectMake(0, 0, 23, 19)]; 
}

[backButton addTarget:self action:@selector(goToHome) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *HomeButton = [[UIBarButtonItem alloc] initWithCustomView: backButton];
[self.navigationItem setLeftBarButtonItem:HomeButton];

您应该通知您说它是一个
UIBarButtonItem
,但您在代码中使用了
UIButton

在ios5.0中,您可以使用
UIAppearence协议
更改
UIBarButtonItem
背景图像,如:


[[UIBarButtonItem appearance] setBackgroundImage: forState: barMetrics:]

您也可以使用elppa的方法,但请注意,您可以直接使用
UIImageView
作为
customView
参数。

您应该通知,您说它是
UIBarButtonItem
,但您在代码中使用了
UIButton

在ios5.0中,您可以使用
UIAppearence协议
更改
UIBarButtonItem
背景图像,如:


[[UIBarButtonItem appearance] setBackgroundImage: forState: barMetrics:]

您也可以使用elppa的方法,但请注意,您可以直接使用
UIImageView
作为
customView
参数。

使用自定义UIButton创建UIBarButton:

_optionsButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_optionsButton setImage:[UIImage imageNamed:@"menu2.png"] forState:UIControlStateNormal];
[_optionsButton addTarget:self action:@selector(optionsButtonPressed) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem * optionsItem = [[UIBarButtonItem alloc] initWithCustomView:_optionsButton];
然后像通常那样为UIBarButton设置背景图像(而不是尝试更改UIBarButton的图像)

这对我很有效

注意:当我看这篇文章时,我正在根据当前图片的内容更改图片。我发现我必须使用:

if ([[_optionsButton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed: @"keyboard.png"]]) {
    // Code for the certain button function 
}

要使其正常工作,请使用自定义UIButton创建UIBarButton:

_optionsButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_optionsButton setImage:[UIImage imageNamed:@"menu2.png"] forState:UIControlStateNormal];
[_optionsButton addTarget:self action:@selector(optionsButtonPressed) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem * optionsItem = [[UIBarButtonItem alloc] initWithCustomView:_optionsButton];
然后像通常那样为UIBarButton设置背景图像(而不是尝试更改UIBarButton的图像)

这对我很有效

注意:当我看这篇文章时,我正在根据当前图片的内容更改图片。我发现我必须使用:

if ([[_optionsButton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed: @"keyboard.png"]]) {
    // Code for the certain button function 
}

要使其工作

viewWillRotate就是上述方法的所在。但它不起作用尝试调用[self.view setNeedsDisplay];在方法的末尾。您正在调用super-viewWillRotateviewWillRotate是上面的方法所在的位置。但它不起作用尝试调用[self.view setNeedsDisplay];在方法的末尾。你是在调用超级视图willrotate这不是在更改UIBarButton它只是在创建一个新的UIBarButton这不是在更改UIBarButton它只是在创建一个新的UIBarButton