Thursday, May 30, 2013

UIButton adjustsImageWhenDisabled example in Objective C (iOS).

UIButton adjustsImageWhenDisabled

A Boolean value that determines whether the image changes when the button is disabled.

@property(nonatomic) BOOL adjustsImageWhenDisabled

Discussion of [UIButton adjustsImageWhenDisabled]
If YES, the image is drawn darker when the button is disabled. The default value is YES.

UIButton adjustsImageWhenDisabled example.
@implementation UIViewController (CustomFeatures)
-(void)setNavigationBar{
    // Set the custom back button
    UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted];
    button.adjustsImageWhenDisabled = NO;


    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, 30, 30);

    [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;
    self.navigationItem.hidesBackButton = YES;

    // Cleanup
    [customBarItem release];
}
@end

Example of [UIButton adjustsImageWhenDisabled].
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame style:(UIButtonType)style

    UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    button.frame = frame;
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
   
    [button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
   
    //[button setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
   
    [button addTarget:target action:inSelector forControlEvents:UIControlEventTouchUpInside];
    button.adjustsImageWhenDisabled = YES;
    button.adjustsImageWhenHighlighted = YES;
       
    [button autorelease];
    return button;

UIButton adjustsImageWhenDisabled example.
    UIButton * button1 = [UIButton buttonWithType:2];
    button1.frame = CGRectMake(20, 20, 30, 30);
    button1.tag = 0;
    button1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:button1];
   
    UIButton *button2 = [UIButton buttonWithType:0];
    button2.frame = CGRectMake(20, 60, 100, 40);
    button2.tag = 0;
    [button2 setTitle"button" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button2.adjustsImageWhenDisabled = NO;
    button2.adjustsImageWhenHighlighted = NO;
    button2.backgroundColor = [UIColor cyanColor];

End of UIButton adjustsImageWhenDisabled example article.