Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何定制UINavBar?_Iphone_Objective C_Xcode_Uinavigationcontroller - Fatal编程技术网

Iphone 如何定制UINavBar?

Iphone 如何定制UINavBar?,iphone,objective-c,xcode,uinavigationcontroller,Iphone,Objective C,Xcode,Uinavigationcontroller,我正在使用以下代码向我的应用程序添加自定义导航栏。但我希望能够根据视图更改导航栏,这可能吗 @implementation UINavigationBar (UINavigationBarCategory) - (void)drawRect:(CGRect)rect { UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0]; UIImage *img =

我正在使用以下代码向我的应用程序添加自定义导航栏。但我希望能够根据视图更改导航栏,这可能吗

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect 
{
    UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0];
    UIImage *img  = [UIImage imageNamed: @"navbar.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.tintColor = color;
}
@结束


如果没有,是否可以只向导航栏添加图像而不是标题文本?

我强烈建议不要在UINavBar上使用类别来覆盖
-drawRect
,因为这种设计将在即将发布的iOS版本上被破坏

创建导航栏标题的自定义图像和按钮非常容易。下面是我直接从我的一个应用程序的生产代码中提取的一些代码片段。它可以在导航栏的标题区域中创建自定义图像按钮,但创建图像也同样容易:

- (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中的按钮的外观:

以下是使用图像作为导航栏标题的示例:

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;

谢谢,在1视图中,我想要一个自定义导航栏,上面有图像而不是文本(我可以使用.png)。另一方面,我只需要带有标准文本的自定义导航栏,我该怎么做呢?你可以在容器视图中放置几乎任何你想要的东西——按钮、图像、分段控件。我在容器控件中添加了一个UIButton,但您也可以轻松地添加UIImageView。不确定您的意思--如果无法看到您的代码,您将不得不调试您看到奇怪行为的原因。@Answerbot我们需要为所有页面重复该代码吗?我的意思是,对于所有的视图控制器,我想要那种导航条在哪里?@RajanBalana是的。但从iOS 5.0开始,我们可以使用外观代理修改整个应用程序中控件的外观。下面是一个使用代理的示例: