简体   繁体   English

为所有navigationcontroller和navigationitem设置titleview

[英]Set titleview for all navigationcontroller and navigationitem

[[[UINavigationBar appearance] setTitleView:button]

我试图使用上面的代码行将标题视图设置为所有导航项目的按钮,但是它不起作用。如何通过在appdelegate中编写小代码来设置项目中所有导航栏的标题视图?

Customizing the Appearance of a Navigation Bar 自定义导航栏的外观

it is alright to modify the barStyle, tintColor, and translucent properties, but you must never directly change UIView-level properties such as the frame, bounds, alpha, or hidden properties directly. 可以修改barStyle,tintColor和半透明属性,但是绝对不能直接更改UIView级别的属性,例如框架,边界,alpha或隐藏的属性。

For more detail you can follow apple doc UINavigationBar Class Reference 有关更多详细信息,请遵循apple doc UINavigationBar类参考。

Edit -> 编辑->

I solved your query 我解决了您的查询

[[UINavigationBar appearance] addSubview:yourView];  

Other Navigation Related query 其他导航相关查询

try this dude... 试试这个家伙...

//put whatever object in this view..for example button that you want to put...
UIView* ctrl = [[UIView alloc] initWithFrame:navController.navigationBar.bounds];
ctrl.backgroundColor = [UIColor yellowColor];
ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[navController.navigationBar addSubview:ctrl];

let me know it is working or not!!!! 让我知道它是否有效!!!!

Happy Coding!!! 快乐编码!

This is how you create a navigation controller in addDelegate and set a title to it, you have to add the following code to didFinishLaunchingWithOptions method. 这是在addDelegate中创建导航控制器并为其设置标题的方式,您必须将以下代码添加到didFinishLaunchingWithOptions方法中。 Notice that you need to set a root view for the viewController which will the viewController that will be displayed inside the navigationController. 注意,您需要为viewController设置一个根视图,该根视图将显示在navigationController内部的viewController。

  yourViewController *mainVC = [[yourViewController alloc]init];
  UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:mainVC];
  navigationController1.title = @"title";
[window addSubview:navigationController1.view];

If you use iOS4, you can override drawRect 如果您使用iOS4,则可以覆盖drawRect

@implementation UINavigationBar (UINavigationBarCategory)

-(void)drawRect:(CGRect)rect
{

    UIImageView *itleImageView = //create object
    [self addSubView:titleImageView];
     //set title view in navigation bar
}
@end

iOS 5, iOS 5

if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) {
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){

   UIImageView *itleImageView = //create object
   [self.navigationController.navigationBar addSubView:titleImageView];
    }
} 

i guess this is a right, as i don't have implemented. 我想这是对的,因为我还没有执行。

I wanted to add a logo in every NavigationController, so I made a subclass of UINavigationController and used this in the viewDidLoad -Method 我想在每个NavigationController中添加徽标,所以我做了UINavigationController的子类,并在viewDidLoad -Method中使用了它。

UIImage *logo =[UIImage imageNamed:@"logo"];
CGFloat navWidth = self.navigationBar.frame.size.width;
CGFloat navHeight = self.navigationBar.frame.size.height;

UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(navWidth-logo.size.width - 5,
                                                                      (navHeight - logo.size.height) / 2,
                                                                      logo.size.width,
                                                                      logo.size.height)];

logoView.image = logo;

[self.navigationBar addSubview:logoView];

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

相关问题 将UIBarButtonItem添加到navigationItem.titleView吗? - Add UIBarButtonItem to navigationItem.titleView? 为什么navigationItem.titleView不起作用? - Why isn't the navigationItem.titleView working? NavigationItem Titleview以IOS 7下的导航栏为中心 - 不可能? - NavigationItem Titleview centered on the navigationbar under IOS 7 - impossible? UINavigationController TitleView未从ViewController NavigationItem中显示 - UINavigationController TitleView not displayed from ViewController NavigationItem 将navigationItem titleView的位置更改为左侧 - Changing the position of navigationItem titleView to left side 获取navigationItem.titleView layoutSubviews的正确边界 - Get correct bounds for navigationItem.titleView layoutSubviews 添加到navigationItem.titleView时的UIButton样式 - UIButton style when added to navigationItem.titleView 调整 navigationItem.titleView 的框架? - Adjusting navigationItem.titleView's frame? self.navigationController.navigationItem和self.navigationItem之间的区别 - Difference between self.navigationController.navigationItem and self.navigationItem self.navigationItem.rightBarButtonItem vs self.navigationController.navigationItem.rightBarButtonItem - self.navigationItem.rightBarButtonItem vs self.navigationController.navigationItem.rightBarButtonItem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM