【问题标题】:Custom UINavigationBar with custom height in center自定义 UINavigationBar 居中自定义高度
【发布时间】:2014-10-16 19:50:29
【问题描述】:

我已经查看了几乎所有与自定义NavigationBar 相关的操作系统问题,但无法找到有用的解决方案。请看下面的截图,

红色部分代表导航栏中心的一个图标(小图)。

请提出一些解决方案。提前致谢。

编辑:我想在应用程序中为所有UINavigationBar 实现它。我的意思是在每个视图上

注意:我不知道谁反对我的问题,但我想问那些人的问题。你对我的问题有什么解决办法吗?如果不是,那你为什么要否决我的问题?我将无法以这种方式获得帮助。这是完全错误的。

【问题讨论】:

    标签: ios objective-c cocoa-touch uinavigationbar uistoryboard


    【解决方案1】:

    你可以继承 UINavigationBar。

    #import "VSNavigationBar.h"
    
    
    @interface VSNavigationBar ()
    @property (nonatomic, weak) UIView *noseView;
    
    @end
    
    
    @implementation VSNavigationBar
    
    -(void)layoutSubviews
    {
        [super layoutSubviews];
        static CGFloat width = 80;
        if (!_noseView) {
    
            UIView *noseView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2 - width / 2, 0, width, width)];
            self.noseView = noseView;
    
            self.noseView.backgroundColor = self.barTintColor;
            self.noseView.layer.cornerRadius = self.noseView.frame.size.width / 2;
    
            [self addSubview:_noseView];
        }
        _noseView.frame = CGRectMake(self.bounds.size.width / 2 - width / 2, 0, width, width);
    }
    
    @end
    

    在您的 Storyboard 中,您将选择 NavigationController 场景,在左侧的树视图中选择 Navigation Bar,在右侧选择身份检查器并将类更改为子类。

    【讨论】:

    • 顺便说一句:老实说,我非常讨厌这些设计。
    • 那个鼻子视图中心的标志(图标)怎么样?
    • 它是一个视图,所以你可以添加任何子视图。
    【解决方案2】:

    您需要创建一个看起来像您提供的图像的中心部分的图像,然后说 -

    UIImage *image = [UIImage imageNamed: @"logo.png"];
    UIImageView *imageview = [[UIImageView alloc] initWithImage: image];
    self.navigationItem.titleView = image view;
    

    如果您想获得类似于您提供的图像底部的圆形效果,在这种情况下,您需要拥有与上面相同的图像并将其设置为导航栏的背景图像 -

    [navigationBar setBackgroundImage:[UIImage imageNamed: @"yourImage"] forBarMetrics:UIBarMetricsDefault];
    

    【讨论】:

    • 不工作,我也想为所有UINavigationBar实现它
    猜你喜欢
    • 2013-01-24
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    相关资源
    最近更新 更多