How to make setEnabled on ToolbarItems work with SDK 4.0 -iPhone

288 views Asked by At

I have added a custom button to a toolbar and have set the property setEnabled:NO but the button does not shows up greyed out when the view loads using SDK 4.0. However if the same code is compiled with SDK 3.1.2 the button shows up greyed out. Any ideas?

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    UIImage* pImage = [UIImage imageNamed:@"Test.png"];

    CGRect TestBtnFrame = CGRectMake(0, 0, pImage.size.width, pImage.size.height);
    UIButton* pTestBtn = [[UIButton alloc] initWithFrame:TestBtnFrame];

    [pTestBtn setTitle:@"  Test" forState:UIControlStateNormal];
    [pTestBtn addTarget:self action:@selector(OnTestBtnClick)   forControlEvents:UIControlEventTouchUpInside];      
    [pTestBtn setBackgroundImage:image forState:UIControlStateNormal];

    // [pTestBtn setEnabled:NO];     

    // Class member variable to be released in dealloc
    m_pTestBarBtn = [[UIBarButtonItem alloc] initWithCustomView: pTestBtn];
    [pTestBtn release]; 

    [m_pTestBarBtn setEnabled:NO];

    [self setToolbarItems:[NSArray arrayWithObjects: m_pTestBarBtn, nil] animated:YES];

}
1

There are 1 answers

1
Macmade On

You forgot to set the background image for the disabled state...

[ pTestBtn setBackgroundImage: **imageDisabled** forState: UIControlStateDisabled ];