2014-01-16 67 views
0

我想爲iOS 7自定義應用程序的UI。我想要的是iOS 7的BackBarButton,但是箭頭和標題以及標題的不同字體使用了不同的顏色。這是iOS 7的後退按鈕。如何爲BackBarButton的左欄按鈕製作自定義的UIBarButton

BackBarButton of iOS 7

我試着用下面的代碼

UIImage *backButtonImage = [UIImage imageNamed:@"back.png"]; 
    UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [customBackButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 
    [customBackButton setBackgroundImage:backButtonImage forState:UIControlStateNormal]; 
    [customBackButton setFrame:CGRectMake(0, 0, 30, 30)];  
    backButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton]; 
    [backButton setAction:@selector(backButtonPressed)]; 
    UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:customBackButton]; 

這似乎是這樣來定製它:

Customized BackBarButton

在兩個問題上面的圖像。

首先它沒有對齊到左邊(iOS後退按鈕粘到左邊)。

其次,它沒有前一頁的標題。

我只是將標題屬性添加到自定義按鈕。

[customBackButton setTitle:@"title" forState:UIControlStateNormal]; 

但它是這樣的:

customized backBarButton and it's title

我怎樣才能解決這些問題(粘到左和標題)?

回答

0

我剛剛找到了它是如何可能

我寫了下面的代碼,它完美地工作!

UIBarButtonItem * backButton = [UIBarButtonItem new]; 
UIView * backButtonView = [UIView new]; 
[backButtonView setFrame:CGRectMake(0, 0, 90, 32)]; 

UIImageView *backButtonImage = [UIImageView new]; 
[backButtonImage setImage:[UIImage imageNamed:@"Setting/back.png"]]; 
[backButtonImage setFrame:CGRectMake(-15, 0, 30, 30)]; 

UILabel * backButtonLabel = [UILabel new]; 
[backButtonLabel setFrame:CGRectMake(8, 0, backButtonView.frame.size.width, 30)]; 
[backButtonLabel setBackgroundColor:[UIColor clearColor]]; 
[backButtonLabel setTextColor:[UIColor whiteColor]]; 
[backButtonLabel setTextAlignment:NSTextAlignmentLeft]; 
[backButtonLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18]]; 
[backButtonLabel setText:title]; 

UIButton *customBackButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[customBackButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 
[customBackButton setFrame:CGRectMake(0, 0, 90, 30)]; 

[backButtonView addSubview:customBackButton]; 
[backButtonView addSubview:backButtonImage]; 
[backButtonView addSubview:backButtonLabel]; 

backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView]; 
[backButton setAction:action]; 
[self.navigationItem setLeftBarButtonItem:backButton animated:YES]; 
0

爲此,您需要有一個帶有箭頭&標題的自定義圖像。 如果您想自定義標題,你可以使用下面的代碼>

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"My Title" style:UIBarButtonItemStylePlain target:nil action:nil]; 

但是,如果你希望它的圖像集,你需要的圖像與具有後退按鈕圖像的左側。

圖片:enter image description here

2X圖片:enter image description here

參見編輯器中的圖像,你可以看到,圖像大小爲70x32和方向更趨向左端。你也可以用文字圖像&來使用它。

希望有所幫助。

相關問題