导航栏中的自定义文本
Custom text in navigation bar
我想在 UINavigationBar
中 leftBarButtonItem
/rightBarButtonItem
的位置设置文本。你必须使用 UIBarButtonItem
,但你可以将不同的 UIView
objects 放入其中。首先我尝试使用标签,但那不起作用。代码在 C# 中,但你应该明白了:
UILabel textLabel = new UILabel();
textLabel.Text = "My custom text which should be displayed in navigation bar";
UIBarButtonItem customBarButtonItem = new UIBarButtonItem (textLabel);
NavigationItem.RightBarButtonItem = customBarButtonItem;
标签未显示。如果我使用按钮,它似乎可以工作(除了必须调整样式)。
UIBarButtonItem customBarButtonItem = new UIBarButtonItem ("My custom text which should be displayed in navigation bar", UIBarButtonItemStyle.Plain,null);
NavigationItem.RightBarButtonItem = customBarButtonItem;
为什么不能使用 UILabel
?在 UIBarButtonItem
中显示像 header 这样的文本的正确方法是什么?
编辑:
我目前的发现是:
要么设置UIBarButtonItem
的标题,要么设置UILabel
的框架(感谢Anbu.Karthik)。
改变
UILabel testLabel = new UILabel();
testLabel.frame=CGRectMake(0, 0, 200, 21);
testLabel.Text = "yuhu";
UIBarButtonItem test = new UIBarButtonItem (testLabel.text);
更改这行代码UIBarButtonItem test = new UIBarButtonItem (testLabel);
UILabel testLabel = new UILabel();
testLabel.Text = "yuhu";
testLabel.frame=CGRectMake(x, y, width, height);
//change this line
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:testLabel];
NavigationItem.RightBarButtonItem = test;
使用这个
UIImage *buttonImage = [UIImage imageNamed:@"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
我想在 UINavigationBar
中 leftBarButtonItem
/rightBarButtonItem
的位置设置文本。你必须使用 UIBarButtonItem
,但你可以将不同的 UIView
objects 放入其中。首先我尝试使用标签,但那不起作用。代码在 C# 中,但你应该明白了:
UILabel textLabel = new UILabel();
textLabel.Text = "My custom text which should be displayed in navigation bar";
UIBarButtonItem customBarButtonItem = new UIBarButtonItem (textLabel);
NavigationItem.RightBarButtonItem = customBarButtonItem;
标签未显示。如果我使用按钮,它似乎可以工作(除了必须调整样式)。
UIBarButtonItem customBarButtonItem = new UIBarButtonItem ("My custom text which should be displayed in navigation bar", UIBarButtonItemStyle.Plain,null);
NavigationItem.RightBarButtonItem = customBarButtonItem;
为什么不能使用 UILabel
?在 UIBarButtonItem
中显示像 header 这样的文本的正确方法是什么?
编辑:
我目前的发现是:
要么设置UIBarButtonItem
的标题,要么设置UILabel
的框架(感谢Anbu.Karthik)。
改变
UILabel testLabel = new UILabel();
testLabel.frame=CGRectMake(0, 0, 200, 21);
testLabel.Text = "yuhu";
UIBarButtonItem test = new UIBarButtonItem (testLabel.text);
更改这行代码UIBarButtonItem test = new UIBarButtonItem (testLabel);
UILabel testLabel = new UILabel();
testLabel.Text = "yuhu";
testLabel.frame=CGRectMake(x, y, width, height);
//change this line
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:testLabel];
NavigationItem.RightBarButtonItem = test;
使用这个
UIImage *buttonImage = [UIImage imageNamed:@"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;