UINavigationControllerのタイトルの横に小さなアイコンを表示したいと思います。 Photoshopのマジックを通じてUINavigationControllerにタイトルのある画像を追加する
は、次のように:
私は新しいビューを作成し、そこに画像とタイトルを構築する必要があります知っています。ここで私はやっているものです:
- (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のフレームサイズを接続すると、フレームが表示されますが、すべてが一緒に押しつぶされています。しかし、あなたはそれを見る。私はタイトルが確実に表示されるようにダイナミックでなければなりません。
しかし、私はそれを把握することはできません。
助けが必要ですか?
'sizeToFit'を使用してください。 https://developer.apple.com/reference/uikit/uiview/1622630-sizetofit?language=objcのドキュメントは次のとおりです。 –
ナビゲーションバーはビューを自動サイズ変更しますか?もしそうなら、XIBでこれを作って、サイズを変更したときの見た目を簡単に見ることができるかもしれません。 –