簡體   English   中英

為所有navigationcontroller和navigationitem設置titleview

[英]Set titleview for all navigationcontroller and navigationitem

[[[UINavigationBar appearance] setTitleView:button]

我試圖使用上面的代碼行將標題視圖設置為所有導航項目的按鈕,但是它不起作用。如何通過在appdelegate中編寫小代碼來設置項目中所有導航欄的標題視圖?

自定義導航欄的外觀

可以修改barStyle,tintColor和半透明屬性,但是絕對不能直接更改UIView級別的屬性,例如框架,邊界,alpha或隱藏的屬性。

有關更多詳細信息,請遵循apple doc UINavigationBar類參考。

編輯->

我解決了您的查詢

[[UINavigationBar appearance] addSubview:yourView];  

其他導航相關查詢

試試這個家伙...

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

讓我知道它是否有效!!!!

快樂編碼!

這是在addDelegate中創建導航控制器並為其設置標題的方式,您必須將以下代碼添加到didFinishLaunchingWithOptions方法中。 注意,您需要為viewController設置一個根視圖,該根視圖將顯示在navigationController內部的viewController。

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

如果您使用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

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

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

我想這是對的,因為我還沒有執行。

我想在每個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM