[英]UINavigationBar title view alignment issue
我已經將ImageView
設置為特定ViewController的標題視圖,而其他控制器將UILabel
為titleView。 我使用以下代碼設置標題視圖,
UIImageView *imageTitle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo"]];
[self.navigationController.navigationBar.topItem setTitleView:imageTitle];
我也嘗試過使用這個
UIImageView *imageTitle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo"]];
[self.navigationItem setTitleView:imageTitle];
問題是,當我彈出視圖控制器時,標題視圖會在左側停留幾秒鍾。 我不確定這是什么原因? 請讓我知道我要去哪里錯了嗎??? !
PS:該問題僅發生在iOS 7中!
這是存在此問題的示例項目-https: //dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView.zip
更新
正如@FahimParkar所建議的那樣,我使用了320x44的圖片作為titleview。 由於圖像較寬,定位延遲似乎很小。 這是此問題的唯一解決方案嗎?
鏈接下載具有320x44圖像的試用項目-> https://dl.dropboxusercontent.com/u/97646145/Issue/NavigationTitleView_Updated.zip
在您的ViewWillAppear
方法中嘗試以下代碼,希望對您ViewWillAppear
幫助:
UIImage *image = [UIImage imageNamed: @"Logo"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
您有兩個選擇可以正確設置導航。
第一:
像下面一樣設置Titleview:-
- (void)viewDidLoad
{
UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.center=navView.center; // this is display your image at center of navigation bar.
imgviw.image=[UIImage imageNamed:@"passowrd.png"];
[navView addSubview:imgviw];
self.navigationItem.titleView = navView;
[super viewDidLoad];
}
該輸出像:
第二:
將導航圖像設置為特定view-controller
要求。 創建具有與導航欄相同大小的導航圖像。 並使用貝婁代碼進行設置。
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Navbar.png"] forBarMetrics:UIBarMetricsDefault];
可能存在的問題autoresizing
UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LOGO.png"]];
imgView.autoresizingMask=UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imgView.contentMode=UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = imgView;
您需要在viewDidLoad
而不是viewWillAppear
設置導航viewDidLoad
標題視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.