2014-05-10 33 views
3

我正在嘗試滑動菜單。唯一的問題是UIButton的圖像在運行時不顯示。這是我寫的代碼。UIButton圖像不顯示在導航欄中

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 
-(NSString *)segueIdentifierForIndexPathInLeftMenu:(NSIndexPath *)indexPath 
{ 
    NSString *identifier; 
    switch (indexPath.row) { 
     case 0: 
      [email protected]"firstSegue"; 
      break; 
      case 1: 
      [email protected]"secondSegue"; 
      break; 
    } 
    return identifier; 
} 
-(void)configureLeftMenuButton:(UIButton *)button 
{ 
    CGRect frame = button.frame; 

    frame.origin=(CGPoint){0.0}; 
    frame.size = (CGSize){40.40}; 
    button.frame = frame; 
    [button setImage:[UIImage imageNamed:@"icon-menu"] forState:UIControlStateNormal]; 
} 
+0

此代碼是不夠的..您是否已將按鈕添加到視圖? –

+0

不,我沒有向故事板添加按鈕。我做了實驗,但仍然無法顯示! – user2167323

+0

剛剛完成。 – user2167323

回答

1

我想你正在使用導航控制器。如果是這樣,實現可能看起來像這樣:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self configureLeftMenuButton]; 
} 

-(void)configureLeftMenuButton 
{ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setImage:[UIImage imageNamed:@"icon-menu"] forState:UIControlStateNormal]; 
    button.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f); 

    [button addTarget:self 
       action:@selector(yourAction:) 
    forControlEvents:UIControlStateNormal]; 

    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

    self.navigationItem.leftBarButtonItem = menuItem; 
}