0

UINavigationControllerのタイトルの横に小さなアイコンを表示したいと思います。 Photoshopのマジックを通じてUINavigationControllerにタイトルのある画像を追加する

は、次のように:

enter image description here

私は新しいビューを作成し、そこに画像とタイトルを構築する必要があります知っています。ここで私はやっているものです:

- (void) setTitleBar { 


    CGRect navBarFrame = self.navigationController.navigationBar.frame; 


    //UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(navBarFrame.origin.x, navBarFrame.origin.y, (leftButtonFrame.origin.x + leftButtonFrame.size.width) - rightButtonFrame.origin.x, navBarFrame.size.height)]; 
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(navBarFrame.origin.x, navBarFrame.origin.y,self.view.frame.size.width,navBarFrame.size.height)]; 
    titleView.backgroundColor = [UIColor clearColor]; 


    CGPoint tvCenter = CGPointMake(titleView.frame.size.width/2, titleView.frame.size.height/2); 

    UIImage * icon = [UIImage imageNamed:@"star"]; 

    UIImageView *iconView = [[UIImageView alloc] initWithImage:icon]; 
    iconView.frame = CGRectMake(0, 0, icon.size.width, icon.size.height); 

    UILabel *title = [[UILabel alloc] init]; 
    title.backgroundColor = [UIColor clearColor]; 
    title.textColor = [UIColor whiteColor]; 
    title.textAlignment = NSTextAlignmentCenter; 
    title.text = @"SOME TITLE"; 
    title.frame = CGRectMake(0, 0, 100, titleView.frame.size.height); 
    [title sizeToFit]; 


    iconView.center = CGPointMake(tvCenter.x - (icon.size.width/2), tvCenter.y); 

    [titleView addSubview:iconView]; 
    [titleView addSubview:title]; 

    self.navigationItem.titleView = titleView; 
} 

titleViewでの私のロジックは次のとおりです。

をのviewDidLoadでUINavigationControllerビューコントローラでは、私が

、このメソッドを呼び出します

[self setTitleBar];

メソッドを呼び出します:最も左のボタンのフレームを取得し、最も右のボタンのフレームを取得します。次に、どの程度の大きさの視野が得られるかを数学で計算します。 titleViewのフレームサイズにする必要があります。

しかし、私はそれを動作させるように見えることはできません。私が0,0,100,40のフレームサイズを接続すると、フレームが表示されますが、すべてが一緒に押しつぶされています。しかし、あなたはそれを見る。私はタイトルが確実に表示されるようにダイナミックでなければなりません。

しかし、私はそれを把握することはできません。

助けが必要ですか?

+0

'sizeToFit'を使用してください。 https://developer.apple.com/reference/uikit/uiview/1622630-sizetofit?language=objcのドキュメントは次のとおりです。 –

+0

ナビゲーションバーはビューを自動サイズ変更しますか?もしそうなら、XIBでこれを作って、サイズを変更したときの見た目を簡単に見ることができるかもしれません。 –

答えて

0

オブジェクトをナビゲーションコントローラビューにサブビューとして配置できます。

- (void) setTitleBar { 

    //Let's say your icon size is 20 
    int starSize = 20; 
    //Now you'll have to calculate where to place the ImageView respect the TextSize (for this you'll need to know the text and font of your UINavigationItem title) 
    CGSize textSize = [@"SOME TITLE" sizeWithAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"navfontname" size:15]}]; 
    UIImageView *startImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.navigationController.view.frame.size.width/2 - textSize.width/2, self.navigationController.view.frame.size.height/2 - starSize/2, starSize,starSize)]; 
    startImageView.image = [UIImage imageNamed:@"star"]; 
    [self.navigationController.view addSubview:startImageView]; 

} 
+0

ええと...私はこれが動作するように見えることはできません。論理的だと思われますが –

+0

UIImageViewはself.navigationController.viewのサブビューとして追加されていますか?そのフレームをCGRectMake(0,0,20,20)のようなものに設定すればそれを見ることができますか? –

+0

画面の中央に配置します。 –

関連する問題