UIButton图像未显示在导航栏中

时间:2014-05-10 22:32:34

标签: ios objective-c 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:
            identifier=@"firstSegue";
            break;
            case 1:
            identifier=@"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];
}

1 个答案:

答案 0 :(得分: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;
}