ios 自定义带有标题视图的导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8433016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customize navigation bar with title view
提问by Julian Osorio
I am trying to add a custom view in the center of a navigation bar and I am using the following code to test it:
我正在尝试在导航栏的中心添加自定义视图,并使用以下代码对其进行测试:
UIView * testView = [[UIView alloc] init];
[testView setBackgroundColor:[UIColor blackColor]];
testView.frame = CGRectMake(0, 0, 100, 35);
[self.navigationController.navigationItem.titleView addSubview:testView];
I am setting this up in the viewDidLoad method of my view controller but when i run my program nothing seems to change in my navigation bar.
我在我的视图控制器的 viewDidLoad 方法中进行了设置,但是当我运行我的程序时,我的导航栏中似乎没有任何变化。
Could you help me with this?
你能帮我解决这个问题吗?
回答by virata
This works. Give frame at the time of initialisation
这有效。初始化时给出帧
UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.titleView = iv;
回答by Raphael Oliveira
If you want to just customize the title for one view controller you can use
如果您只想自定义一个视图控制器的标题,您可以使用
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = @"Diga-nos o motivo";
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0];
lblTitle.shadowColor = [UIColor whiteColor];
lblTitle.shadowOffset = CGSizeMake(0, 1);
lblTitle.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0];
[lblTitle sizeToFit];
self.navigationItem.titleView = lblTitle;
or if you want to customize for all view controllers use
或者如果您想为所有视图控制器自定义使用
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Arial-Bold" size:10.0],
UITextAttributeFont,
nil]];
回答by Kjuly
Replace
代替
[self.navigationController.navigationItem.titleView addSubview:testView];
to
到
self.navigationItem.titleView = testView;
Edit:
编辑:
Note: You cannot add subviews to titleView cause it's default value is nil
, you need to set a new view as the titleView.
注意:你不能向 titleView 添加子视图,因为它的默认值为nil
,你需要设置一个新视图作为 titleView。
回答by Guru Murthy
CustomLabel *titleLabel = [CustomLabel initWithLabelFrame:labelFrame textFont:[UIFont fontWithName:@"Helvetica" size:[UIFont systemFontSize]] textColor:[UIColor blackColor] labelText:@"Add as" textAlignment:NSTextAlignmentCenter labelOnView:reference.view labelTag:62];
[self.navigationItem setTitleView:titleLabel]; // titleLabel set in navigationItem
回答by Durul Dalkanat
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController
@end
#import "MasterViewController.h"
@interface MasterViewController ()
@end
@implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self titleView];
}
- (UIView *)titleView {
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat width = 0.95 * self.view.frame.size.width;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, navBarHeight)];
UIImage *logo = [UIImage imageNamed:@"logo.png"];
UIButton *logoButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat logoY = floorf((navBarHeight - logo.size.height) / 2.0f);
[logoButton setFrame:CGRectMake(0, logoY, logo.size.width, logo.size.height)];
[logoButton setImage:logo forState:UIControlStateNormal];
UIImage *bubble = [UIImage imageNamed:@"notification-bubble-empty.png"];
UIImageView *bubbleView = [[UIImageView alloc] initWithImage:bubble];
const CGFloat Padding = 5.0f;
CGFloat bubbleX =
logoButton.frame.size.width +
logoButton.frame.origin.x +
Padding;
CGFloat bubbleY = floorf((navBarHeight - bubble.size.height) / 2.0f);
CGRect bubbleRect = bubbleView.frame;
bubbleRect.origin.x = bubbleX;
bubbleRect.origin.y = bubbleY;
bubbleView.frame = bubbleRect;
[containerView addSubview:logoButton];
[containerView addSubview:bubbleView];
return containerView;
}
@end
回答by nikans
Swift 3/4
斯威夫特 3/4
You may set i.e. UILabel
as a titleView
. Call it in viewDidLoad()
:
您可以将 ie 设置UILabel
为titleView
. 调用它viewDidLoad()
:
private func setNavigationTitle(_ title: String) {
navigationItem.title = nil // clear the default title
let titleLabel = UILabel() // you don't need to specify a frame, it will be centred in the navbar
titleLabel.font = ...
titleLabel.textColor = ...
titleLabel.text = title
titleLabel.backgroundColor = .clear
navigationItem.titleView = titleLabel
navigationTitleView = titleLabel // you may create a property if you want to manipulate the title view later
}
NotenavigationItem.title = nil
, otherwise title
may override titleView
.
注意navigationItem.title = nil
,否则title
可能会覆盖titleView
。
回答by Krunal
Swift 3
斯威夫特 3
let myImageView = UIImageView(image: <...set your image...>)
override fun viewDidLoad(){
super.viewDidLoad()
self.navigationItem.titleView = myImageView //
}
One more alternate solution is
另一种替代解决方案是
override fun viewWillAppear(_ animated: Bool) {
super. viewWillAppear(animated)
self.navigationItem.titleView = myImageView
}
I recommend to use, viewDidLoad
to setup your titleView
我建议使用,viewDidLoad
设置你的titleView
回答by Ray W
You'll want to use storyboard to do this to support iPhone 6 and newer (larger) devices.
您需要使用情节提要来执行此操作以支持 iPhone 6 和更新(更大)的设备。
Create a container view for your custom navigation item title/subtitle etc, and drag it into the visual editor (not into the view hierarchy).
为您的自定义导航项标题/副标题等创建一个容器视图,并将其拖入可视化编辑器(而不是视图层次结构中)。