簡體   English   中英

設置導航欄標題的對齊方式

[英]set the alignment of title of navigation bar

我正在嘗試將導航欄的標題與我的應用程序中心對齊,但似乎標題是留在右側(請找到屏幕截圖)。 我正在使用以下代碼:

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

當我隱藏后退按鈕時,標題顯示中心對齊的標題。 是否有任何方法可以設置后退按鈕的外觀屬性?

圖像鏈接在這里

任何人都可以指導我可能出錯的地方。

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

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

你很高興去! 此代碼將自動將其自身置於導航欄的中心。

或者,如果您實際擁有UINavigationController的實例,請使用:

[self setTitle:@"Your Title"];

您需要在上一個viewcontroller的viewWillDisappear方法中設置self.title =“”。

例如。 ViewControllerB從ViewControllerA打開,然后在ViewControllerA的“viewWillDisappear”方法中設置self.title =“”

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

只需在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;
}

如果你有一個導航控制器,那么只需添加:self.title = @“你的標題”;

嘗試使用self.navigationItem.title = @“YourTitle”; 此代碼將標題放在導航欄的中心。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM