Skip to content

Instantly share code, notes, and snippets.

@convexstyle
Last active December 21, 2015 00:09
Show Gist options
  • Save convexstyle/6218293 to your computer and use it in GitHub Desktop.
Save convexstyle/6218293 to your computer and use it in GitHub Desktop.
Add a custom title to a toolbar when navigation bar is not available.
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32.0)];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backHandler:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.text = @"Sample Title";
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
titleLabel.shadowOffset = CGSizeMake(0, -1.0);
[titleLabel sizeToFit];
UIBarButtonItem *labelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Right Button" style:UIBarButtonItemStyleBordered target:self action:@selector(rightButtonHandler:)];
toolbar.items = [NSArray arrayWithObjects:backButtonItem, flexibleSpace, labelButtonItem, flexibleSpace, rightBarButtonItem, nil];
[self.view addSubView:toolbar];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment