简体   繁体   English

UIBackButton背景图像没有出现

[英]UIBackButton Background Image not appearing

i'm having a problem with my UIBackbutton not appearing and i'm not sure what is causing it. 我的UIBack按钮没有出现问题,我不确定是什么导致它。

This is my code for making the back button image, it is in my first ViewController : 这是我制作后退按钮图像的代码,它在我的第一个ViewController中:

UIImage *backButtonHomeImage = [[UIImage imageNamed:@"backButtonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

When you first enter the next view (done by a segue from a button) the button isn't visible but the text to go back is visible. 当您第一次进入下一个视图(由按钮中的segue完成)时,该按钮不可见,但可以看到要返回的文本。 Once i've hit the back button the image appears and it stays appeared on the next time i click it. 一旦我点击后退按钮,图像就会出现,并在下次单击时保持显示状态。

I'm not sure if this is an issue to do with my code, or the file it's in or if it's and iOS 7 problem. 我不确定这是否与我的代码或它所在的文件或者它和iOS 7问题有关。

You may have more luck using a UIBarButtonItem and setting the back button background explicitly. 您可以使用UIBarButtonItem并明确设置后退按钮背景。

The appearance proxy has been unreliable in my own attempts to get a back button working with iOS 7. I experienced the same issue with the background not showing up properly first time showing the button. 外观代理在我自己尝试使用iOS 7的后退按钮时一直不可靠。我遇到了相同的问题,背景没有正确显示第一次显示按钮。

如果你只需要改变颜色,试试这个[[UINavigationBar appearance] setTintColor:[UIColor redColor]];

If you want to change back button image you just need to write this 2 lines of code: 如果你想改变后退按钮图像,你只需要编写这两行代码:

[self.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"icon-back"]];
[self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"icon-back"]];

And if you create sub class of UINavigationController it makes easy to customize you navigationbar and push & pop controllers 如果您创建UINavigationController的子类,它可以轻松自定义导航栏和推送和弹出控制器

Try this: 尝试这个:

self.navigationItem.hidesBackButton = NO;

if you're trying to add it to the Navigation bar then: 如果您尝试将其添加到导航栏,则:

 UIImage* image4 = [UIImage imageNamed:@".....png"];
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
    [button setBackgroundImage:....png forState:UIControlStateNormal];
    [button addTarget:self action:@selector(Selector)
         forControlEvents:UIControlEventTouchUpInside];
    [button setShowsTouchWhenHighlighted:YES];

    UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithCustomView:button];
    NSArray *arr =[[NSArray alloc]initWithObjects:action, nil];
    self.navigationItem.rightBarButtonItems = arr;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM