Я бы настоятельно не рекомендовал использовать категорию для переопределения -drawRect
на UINavBar, так как этот дизайн будет ломаться в скором выпуске iOS-версии.
Очень легко создавать собственные изображения и кнопки для заголовка navBar. Вот некоторые фрагменты кода, которые я извлек непосредственно из рабочего кода для одного из моих приложений. Он создает пользовательскую кнопку изображения в области заголовка navBar, но вместо этого легко создать изображение:
- (void) viewDidLoad {
UIButton *titleButton = [self buttonForTitleView];
[titleButton setTitle:newTitle forState:UIControlStateNormal];
[titleButton sizeToFit];
self.navigationBar.topItem.titleView = titleButton;
}
- (UIButton *) buttonForTitleView {
if (!_buttonForTitleView) {
UIImage *buttonImage = [UIImage imageNamed:@"button_collection_name2"];
UIImage *pressedButtonImage = [UIImage imageNamed:@"button_collection_name2_pressed"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: buttonImage forState : UIControlStateNormal];
[button setBackgroundImage: pressedButtonImage forState : UIControlStateHighlighted];
[button setFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.minimumFontSize = 10.0;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
button.titleLabel.shadowOffset = (CGSize){ 0, 1 };
[button addTarget:self action:@selector(presentWidgetCollectionSwitchingViewController:) forControlEvents:UIControlEventTouchUpInside];
_buttonForTitleView = [button retain];
}
return _buttonForTitleView;
}
А вот так выглядит кнопка в приложении VideoBot:
![VideoBot custom navbar title button](https://i.stack.imgur.com/Q1EoQ.png)
Вот пример использования изображения для заголовка панели навигации:
UIImage *titleImage = [UIImage imageNamed:@"navbar_title_image"];
UIImageView *titleImageView = [[[UIImageView alloc] initWithImage:titleImage] autorelease];
UIView *container = [[[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, titleImage.size.width, titleImage.size.height}] autorelease];
container.backgroundColor = [UIColor clearColor];
[container addSubview:titleImageView];
// add the view to the title
self.navigationBar.topItem.titleView= container;