2013-11-01 4 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

Вот код, который я использовал для создания пользовательского BackButton

- (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 Спасибо чувак вот обновку для меня ... Спасибо большое чувак .... –

Смежные вопросы