5

我创建了自定义导航控制器,

我要添加,左边是日期,右边是返回按钮,标题旁边是返回按钮。

我尝试添加一个标签,但它不起作用。请告诉我一个方法

 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];
4

5 回答 5

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];

试试这个它会工作。它对我有用:)

于 2013-07-12T09:21:15.663 回答
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
于 2013-07-12T09:57:23.430 回答
2

您也可以从Interface Builder

1.首先添加一个UIView如下所示的

在此处输入图像描述

2.然后在UILabel里面添加一个UIView如下所示

在此处输入图像描述

3.在中心它将如下所示

在此处输入图像描述

现在添加必要的约束以满足您的要求。

于 2018-04-25T10:32:02.050 回答
0

您需要更改titleView. topItem像这样:

naviBarObj.topItem.titleView = navLabel;

** 更新:完整的代码(经过测试和工作)**

UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.titleView = titleLbl;

[naviBar pushNavigationItem: navItem animated: NO];
[navItem release]; // you don't need this for ARC

[self.view addSubview: naviBar];
[naviBar release]; // you don't need this for ARC
于 2013-07-12T09:02:44.943 回答
0

您需要将导航项添加到导航栏。每个导航级别一个。所以你应该至少有一个。

** 更新 **

UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:@"Some title"];

/*
 * add buttons and labels
 */

[self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO];
于 2013-07-12T09:04:19.620 回答