2012-05-17 73 views
1

我有一個自定義酒吧按鈕項目,我想輕推5個像素。我將如何處理下面的代碼。我試圖設置「y」點,但不斷收到語法錯誤。如何移動導航欄內的酒吧按鈕項目

 [self.navigationController setNavigationBarHidden:NO animated:YES]; 

    UIImage *image = [UIImage imageNamed:@"arrow_back.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

    [self.navigationController.navigationBar addSubview:imageView]; 

    [imageView release]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setImage:buttonImage forState:UIControlStateNormal]; 

    //set the frame of the button to the size of the image (see note below) 
    button.frame = CGRectMake(0, 0, buttonImage.size.width, 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; 

    [customBarItem release]; 
} 

感謝您的幫助

+0

哪裏是按鈕畫面申報?我沒有在上面的代碼中看到。可能是這個問題? –

+0

你正在得到什麼語法錯誤? – Maulik

回答

3

一個UIBarButton項目不從UIView的繼承所以沒有「幀」的屬性,所以你需要使用不同的方法,你可以直接調整。

嘗試:

[self.navigationItem.leftBarButtonItem setBackgroundVerticalPositionAdjustment:5 forBarMetrics:UIBarMetricsDefault]; 
+6

我想知道爲什麼沒有相應的setBackgroundHorizo​​ntalPositionAdjustment ... – Morkrom