Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 设置导航栏标题的对齐方式_Iphone_Objective C_Ios_Cocoa Touch - Fatal编程技术网

Iphone 设置导航栏标题的对齐方式

Iphone 设置导航栏标题的对齐方式,iphone,objective-c,ios,cocoa-touch,Iphone,Objective C,Ios,Cocoa Touch,我正在尝试将导航栏的标题与我的应用程序中的中心对齐,但标题似乎位于右侧(请查找屏幕截图)。我正在使用下面的代码: -(void)viewDidLoad { UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStylePlain target:self action:@selector(goToHomePage:)]; [self.navi

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

-(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];
}
视图中将出现()
,如下所示

[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;
当我隐藏“后退”按钮时,标题显示居中对齐的标题。是否有任何方法设置后退按钮的外观属性


谁能告诉我哪里出了问题。

请在ViewDidLoad中调用此命令

- (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;
}
- (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;
}

如果您有一个导航控制器,那么只需添加:self.title=@“您的标题”

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

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

[self.navigationItem setTitle:@"Your Title"];
你可以走了!此代码将自动将其自身放置在导航栏的中心

或者,如果您实际拥有UINavigationController的实例,请使用:

[self setTitle:@"Your Title"];

您需要在上一个viewcontroller的ViewWillEnglishe方法中设置self.title=“”


例如,ViewControllerB正在从ViewControllerA打开,然后在ViewControllerA的“ViewWillEnglishe”方法中设置self.title=“”

我也有同样的问题,但它对我无效,我得到了相同的结果,如所讨论的图像所示。