Tuesday, June 11, 2013

UIToolbar setTranslucent example in Objective C (iOS).

UIToolbar setTranslucent

A Boolean value that indicates whether the toolbar is translucent (YES) or not (NO).

@property(nonatomic, assign, getter=isTranslucent) BOOL translucent

Discussion of [UIToolbar setTranslucent]
Applying translucence to a toolbar is intended primarily for landscape orientation, when you want the user to be able to view the area beneath the toolbar. The default value for this property is NO. However, if you set the toolbar style to UIBarStyleBlackTranslucent, the value for this property is always YES.

UIToolbar setTranslucent example.
The best you can do is using

[helloToolbar setBarStyle:UIBarStyleBlack];
[helloToolbar setTranslucent:YES];
This will get you a black but translucent toolbar.

Example of [UIToolbar setTranslucent].
// Set properties to make background
// translucent.
- (void) applyTranslucentBackground
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    self.translucent = YES;

// Override init.
- (id) init
    self = [super init];
    [self applyTranslucentBackground];
    return self;

// Override initWithFrame.
- (id) initWithFrame:(CGRect) frame
    self = [super initWithFrame:frame];
    [self applyTranslucentBackground];
    return self;

UIToolbar setTranslucent example.
    UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];

    UIBarButtonItem* previous = [[UIBarButtonItem alloc] initWithTitle:@"Anterior" style:UIBarButtonItemStyleBordered target:self action:@selector(move:)];
    UIBarButtonItem* next = [[UIBarButtonItem alloc] initWithTitle:@"Próximo" style:UIBarButtonItemStyleBordered target:self action:@selector(move:)];
    UIBarButtonItem* space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
    UIBarButtonItem* ok = [[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStyleBordered target:self action:@selector(ok:)];

    [toolbar setItems:[[NSArray alloc] initWithObjects:previous, next, space, ok, nil]];

    [toolbar setTranslucent:YES];
    [toolbar setTintColor:[UIColor blackColor]];

End of UIToolbar setTranslucent example article.