简体   繁体   English

UINavigatiobar右barbutton项目图像不可见

[英]UINavigatiobar right barbutton item image not visible

I am creating a custom right bar button item for UINavigation bar. 我正在为UINavigation栏创建一个自定义的右栏按钮项。 Here is the code I am using, 这是我正在使用的代码,

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    UIButton *backPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backPageButton setFrame:CGRectMake(0, 0, 50, 30)];
    [ backPageButton setImage:[UIImage imageNamed:@"left_arrow.png"] forState:UIControlStateNormal];
    backPageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [backPageButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [backPageButton setUserInteractionEnabled:YES];
    [container addSubview:backPageButton];
    UIButton *forwardPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [forwardPageButton setFrame:CGRectMake(30, 0, 50, 30)];
    [ forwardPageButton setImage:[UIImage imageNamed:@"right_arrow.png"] forState:UIControlStateNormal];
    [forwardPageButton addTarget:self action:@selector(forwardButtonAction) forControlEvents:UIControlEventTouchUpInside];
    forwardPageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [forwardPageButton setUserInteractionEnabled:YES];
    [container addSubview:forwardPageButton];

    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];

// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = item;

For some reason the button images are not visible. 由于某些原因,按钮图像不可见。 I added a background color to each button and can see that the buttons are actually added to navigation bar and also the click events are working fine. 我为每个按钮添加了背景色,可以看到按钮实际上已添加到导航栏中,并且单击事件也能正常工作。 But button images are not visible. 但是按钮图像不可见。

How to fix it? 如何解决?

Thanks 谢谢

  1. Of course you've made a double check that images belong to your target? 当然,您已经仔细检查过图像是否属于您的目标?
  2. To debug your views in runtime you could use small but very useful button in Xcode 要在运行时调试视图,可以在Xcode中使用小的但非常有用的按钮

http://i.stack.imgur.com/QFdDL.png

more about this: https://developer.apple.com/library/ios/recipes/xcode_help-debugger/using_view_debugger/using_view_debugger.html 有关此的更多信息: https : //developer.apple.com/library/ios/recipes/xcode_help-debugger/using_view_debugger/using_view_debugger.html

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

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