gpt4 book ai didi

iphone - 更改 UIBarButtonItem UIButton 的图像

转载 作者:行者123 更新时间:2023-12-01 18:30:27 25 4
gpt4 key购买 nike

我有一个 UIBarButtonItem ,它带有一个带有图像的 UIbutton 的自定义 View ,我希望有一个不同的 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 图像,这是为什么呢?

最佳答案

试试这个:-

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];

关于iphone - 更改 UIBarButtonItem UIButton 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9528789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com