我创建了自定义导航控制器,
我想添加,左边是日期,右边是后退按钮,后面按钮旁边是标题。
我尝试添加一个标签,但它不起作用。请告诉我一个方法
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = @"My Text";
[self.navigationController.navigationBar addSubview:navLabel];
[self.view addSubview:naviBarObj];
答案 0 :(得分:6)
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
navLabel.text = @"My Text";
navLabel.textColor = [UIColor redColor];
[naviBarObj addSubview:navLabel];
[navLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:naviBarObj];
试试这个它会起作用。它适用于我:)
答案 1 :(得分:2)
在UIToolbar
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)];
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] init];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
[tools setItems:buttons animated:NO];
UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem
答案 2 :(得分:2)
您也可以从Interface Builder
<强> 1。首先添加UIView
,如下所示
<强> 2。然后在UILabel
内添加UIView
,如下所示
第3。在中心它将如下所示
现在添加必要的约束以满足您的要求。
答案 3 :(得分:0)
您需要更改titleView
的{{1}}。像这样:
topItem
**更新:完整代码(已测试并正常工作)**
naviBarObj.topItem.titleView = navLabel;
答案 4 :(得分:0)
您需要将导航项添加到导航栏。每个导航级别一个。所以你应该至少有一个。
**更新**
UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:@"Some title"];
/*
* add buttons and labels
*/
[self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO];