3

我正在尝试将导航栏的标题与我的应用程序的中心对齐,但标题似乎停留在右侧(请找到屏幕截图)。我正在使用下面的代码:

-(void)viewDidLoad {
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStylePlain target:self action:@selector(goToHomePage:)];
    [self.navigationController.navigationItem.rightBarButtonItem setTarget:self];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
}

viewWillAppear()如下

[self.navigationItem setTitle:offertitle];

也试过了

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 120, 40)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentLeft;
label.textColor =[UIColor whiteColor];
label.text=offertit;
self.navigationItem.titleView = label;

当我隐藏后退按钮时,标题显示居中对齐的标题。有什么方法可以设置后退按钮的外观属性吗?

图片链接在这里

谁能指导我哪里可能出错了。

4

6 回答 6

8

您不必使用 UILabel。您可以直接使用此代码:

[self.navigationItem setTitle:@"Your Title"];

你可以走了!此代码将自动将其自身放置在导航栏的中心。

或者,如果您实际上有一个 UINavigationController 实例,请使用:

[self setTitle:@"Your Title"];
于 2012-06-12T03:43:43.263 回答
1

需要在上一个viewcontroller的viewWillDisappear方法中设置self.title = ""。

例如。ViewControllerB 从 ViewControllerA 打开然后在 ViewControllerA 的 "viewWillDisappear" 方法中设置 self.title = ""

于 2015-10-27T10:50:02.297 回答
0
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // this will appear as the title in the navigation bar
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor yellowColor]; // change this color
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@"PageThreeTitle", @"");
        [label sizeToFit];
    }

    return self;
}
于 2012-06-11T11:34:16.897 回答
0

只需在 ViewDidLoad 中调用它

- (void)prepareNavigationTitle
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-50, 0.0, 240.0, 40.0)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:14.0];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    label.textAlignment = UITextAlignmentLeft;
    label.textColor =[UIColor whiteColor];
    label.text=offertit;
    self.navigationItem.titleView = label;
}
于 2012-06-11T11:58:08.493 回答
0

如果您有导航控制器,则只需添加: self.title=@"Your Title";

于 2012-06-11T12:26:04.883 回答
0

尝试使用 self.navigationItem.title = @"YourTitle"; 此代码将标题放置在导航栏的中心。

于 2012-06-11T12:33:41.237 回答