2014-07-10 220 views
0

我想在導航欄中設置我的圖像而不是標準後退箭頭。它看起來像ios7;自定義後退按鈕; setBackIndicatorImage + setBackIndicatorTransitionMaskImage

UIImage *tmpImage = [UIImage imageNamed:@"back.png"]; 

CGSize newSize = CGSizeMake(12, 22); 
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0f); 
[tmpImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
UIImage *backButtonImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; 
[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault]; 

[[UINavigationBar appearance] setBackIndicatorImage:backButtonImage]; 
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage]; 

問題有時它的工作,有時不。而且它取決於構建,而不是發射。我的意思是,在一個版本中它可能有效,重建後(即使沒有任何改變),它可能不起作用。如果我的後退按鈕在某個版本中起作用,則無需多次啓動應用程序即可運行。

有沒有人有想法如何解決它? 謝謝!我試過沒有使用UIGraphicsGetImageFromCurrentImageContext();我嘗試使用UIGraphicsGetImageFromCurrentImageContext()來嘗試使用UIGraphicsGetImageFromCurrentImageContext()。 結果是一樣的...

回答

0

我試圖從AppDelegate.m做到這一點,但我做不到。 最後,我提出,這樣生成按鈕靜態方法:

+(UIBarButtonItem*) backButton: (id)target selector:(SEL)selector{ 

    UIViewController* controller=(UIViewController*)target; 
    [controller.navigationController setNavigationBarHidden:NO]; 

    UIImage* backImg = [UIImage imageNamed:@"backbtniphonetrimed.png"]; 
    UIButton* backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [backBtn setFrame:CGRectMake(0, 0, backImg.size.width , backImg.size.height)]; 
    [backBtn setImage:backImg forState:UIControlStateNormal]; 
    [backBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; 

    if ([UIImage imageNamed:@"backbtniphonetrimed.png"] != nil){ 
     [backBtn setImage:[UIImage imageNamed:@"backbtniphonetrimed.png"] forState:UIControlStateHighlighted]; 
    } 

    return [[UIBarButtonItem alloc] initWithCustomView:backBtn]; 
} 

然後在viewDidLoad方法控制器I只是把它稱爲並實現回方法。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.navigationItem.leftBarButtonItem=[UIHelper backButton:self selector:@selector(back)]; 
} 

-(void) back{ 
    [self.navigationController popViewControllerAnimated:YES]; 
}