Thursday, May 30, 2013

UIButton imageView example in Objective C (iOS).

UIButton imageView

The button’s image view. (read-only)

@property(nonatomic, readonly, retain) UIImageView *imageView

Discussion of [UIButton imageView]
Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view. For example:

UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.imageView.exclusiveTouch = YES;
The imageView property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.

UIButton imageView example.
+ (id)buttonWithType:(UIButtonType)buttonType {
    MyButton *toReturn = [super buttonWithType:buttonType];
    toReturn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    return toReturn;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    return contentRect;
}

Example of [UIButton imageView].
-(IBAction)animateButton:(id)sender
{
    [iconBobble setAdjustsImageWhenHighlighted:NO];

    NSArray *images = [[NSArray alloc] init];
    images = [NSArray arrayWithObjects:
              [UIImage imageNamed:IMAGE1],
              [UIImage imageNamed:IMAGE2],
              [UIImage imageNamed:IMAGE3],
              [UIImage imageNamed:IMAGE4],
              nil];

    float animationDuration = 1.5;
    iconBobble.imageView.animationImages = images;
    iconBobble.imageView.animationDuration = animationDuration;
    iconBobble.imageView.animationRepeatCount = 1;
    [iconBobble.imageView startAnimating];
    [self performSelector:@selector(stopButtonAnimation) withObject:nil afterDelay:animationDuration];
}
-(void)stopButtonAnimation
{
    [iconBobble.imageView stopAnimating];
}

UIButton imageView example.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    // Try to dequeue
    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    // If nothing to dequeue, create your CustomCell
    if(!cell){
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
        for(id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[CustomCell class]]){
                cell = (CustomCell *)currentObject;
                break;
            }
        }
    }
    UIButton *check = (UIButton*) [cell.contentView viewWithTag:1];
    UIButton *check2 = (UIButton*) [cell.selectedBackgroundView viewWithTag:1];
    UILabel *from = (UILabel*) [cell.contentView viewWithTag:2];
    UILabel *subject = (UILabel*) [cell.contentView viewWithTag:3];

    // Configure the cell...
    Message *rec = (Message*) Records[indexPath.row];
    if(rec.Checked)
        check.imageView.image = [UIImage imageNamed:@"Checkedbox.png"];
    else
        check.imageView.image = [UIImage imageNamed:@"Checkbox.png"];
    check2.imageView.image = check.imageView.image;
    from.text = rec.From;
    subject.text = rec.Subject;
    return cell;
}

End of UIButton imageView example article.