2013-08-06 67 views

回答

1

你可以添加一個自定義titleView到你的導航項目,並放置一個偏離中心的UILabel來保存你的標題。另一個更簡單的方法,可能是在你的標題末尾添加一個或兩個空格字符,如果這給你提供了你需要的位移量。

1

您的代碼在這裏

- (void) customNavBarTitleWithText:(NSString *) text andFontsize:(float) fontsize 
{ 
    // Custom nav bar back button 
    UIImage *img = [UIImage imageNamed:@"back_btn.png"]; 
    UIButton *backbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)]; 
    [backbtn setImage:img forState:UIControlStateNormal]; 
    [backbtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithCustomView:backbtn]; 
    [self.navigationItem setLeftBarButtonItem:backBarBtn]; 

    //Custom nav bar title 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont boldSystemFontOfSize:fontsize]; 
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
    label.textAlignment = NSTextAlignmentCenter; 

    // Use UITextAlignmentCenter for older SDKs. 
    label.textColor = [UIColor whiteColor]; // change this color 
    self.navigationItem.titleView = label; 
    label.text = text; 
    label.lineBreakMode = NSLineBreakByWordWrapping; 
    label.numberOfLines = 0; 
    [label sizeToFit]; 

    // Set nav bar background image 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bar_bg.png"] forBarMetrics:UIBarMetricsDefault]; 
    } 

在那裏:

label.textAlignment = NSTextAlignmentCenter; 

你可以使用另一種模式:NSTextAlignmentLeft,NSTextAlignmentRight,NSTextAlignmentJustified等。

希望它能爲你工作!