2014-05-10 79 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; 
}