2013-11-01 169 views
2

我試圖添加自定義後退按鈕導航欄上,但它沒有在下面的工作是我的代碼添加自定義後退按鈕導航欄上

-(void)addLeftButton 
{ 
    UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"]; 

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 

    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); 

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton]; 

    [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside]; 

    self.navigationItem.backBarButtonItem = aBarButtonItem; 
} 

請告訴什麼是錯,此代碼

回答

5

試試這個。

-(void)addLeftButton 
{ 
    UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"]; 

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 

    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); 

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton]; 

    [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside]; 

    [self.navigationItem setLeftBarButtonItem:aBarButtonItem]; 
} 
+0

這是左鍵不後退按鈕@ Developer.iOS –

+0

它意味着你不希望把後退按鈕在導航的左側? –

+0

日Thnx傢伙,它的工作 –

0

這裏是我用來創建自定義的返回按鈕

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

    // Set the custom back button 

    //add the resizable image 
    UIImage *buttonImage = [[UIImage imageNamed:@"backbtn.png"] 
          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)]; 

    //create the button and assign the image 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal]; 

    MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)]; 
    lbl.text = @"Settings"; 
    lbl.backgroundColor = [UIColor clearColor]; 
    //lbl.font = [UIFont fontWithName:@"Helvetica" size:12.0]; 
    lbl.numberOfLines = 0; 

    lbl.lineHeight = 12; 
    lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle; 
    lbl.font = [UIFont fontWithName:@"Helvetica" size:12]; 

    [button addSubview:lbl]; 

    //set the frame of the button to the size of the image (see note below) 
    button.frame = CGRectMake(0, 0, 70, buttonImage.size.height); 
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; 

    //create a UIBarButtonItem with the button as a custom view 
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    self.navigationItem.leftBarButtonItem = customBarItem; 

} 

-(void)back { 
    // Tell the controller to go back 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

此代碼將調整圖片大小,因此它可以適應文本的代碼。 你也可以用任何方法放置這個按鈕。

0

您沒有將操作添加到您的按鈕。所以你需要添加行動到你的按鈕。欲瞭解更多信息請參見下面的代碼..

-(void)addLeftButton 
{ 
    UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"]; 

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; 

    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); 

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton]; 

    [aButton addTarget:self action:@selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside]; // -------- Edit here ----- 

    self.navigationItem.backBarButtonItem = aBarButtonItem; 
} 

-(void)backBtnPressed 
{ 
     [self.navigationController popViewControllerAnimated:YES]; 
} 
+0

設置動作不事項@Dharambir –

+0

@GaganKumar感謝夥計那新的東西對我來說......感謝很多傢伙.... –

相關問題