将NavigationBar背景设置为纯色

时间:2012-04-24 17:54:54

标签: ios uinavigationbar

有什么办法可以将UINavigationController导航栏的背景设置为纯色?

我知道我可以改变Tint颜色,但这仍然让我感受到渐变/玻璃效果。

任何方式我都可以摆脱它,只是有一个普通的纯色?

5 个答案:

答案 0 :(得分:46)

以下代码还会导致导航栏的纯色:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];

答案 1 :(得分:5)

我认为你必须继承UINavigationBar并覆盖-(void)drawRect:(CGRect)rect

UIColor *colorFlat = /* any UIColor*/
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorFlat CGColor]);
CGContextFillRect(context, rect);

答案 2 :(得分:0)

我曾经用覆盖drawRect方法和填充颜色;但是在iOS7升级之后,它会导致UINavigationBar出现一些问题。如果您编写自己的drawrect方法,即使您调用[super drawRect],它也会更改条​​形的尺寸,最终会得到一个高度为44像素的navigationBar。状态栏保留为空。

为了得到一个纯色导航栏,我使用了一个图像作为背景图像(任何小的纯色图像都可以自己拉伸它)并在UINavigationBar子类的initWithFrame方法中添加这些行:

[self setBackgroundColor:[UIColor clearColor]]     
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"bgimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault]; 

答案 3 :(得分:0)

以下代码对我有用:

<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link href='https://fonts.googleapis.com/css? family=Crimson+Text:400,400italic,600,600italic,700,700italic' rel='stylesheet' type='text/css'>
</head>

<body>
        <div id="headline" style="background-image: url(https://images.unsplash.com/photo-1449710146567-1e282fa41f2f?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=c1bffcef22907f217af2fdbc3e430855);">
        <div id="layer" style="background-color: rgba(0,0,0,0.4)">
            <div id="namelogo">
                <p><a href="../index.html">Peter Dam</a></p>
            </div>
                <div id="vertical-align">
                    <h1 class="posttitle">Why the Unfamiliar is Frightening and Why You Should Face It Anyway</h1>
                    <p class="postinfo">January 4, 2016 &bull; 13 minute read &bull; by Peter Dam</p>
                </div>
        </div>
        </div>

        <div id="content">
            <h1>I have never done this before. My decision to pursue a future as a writer came as a surprise to myself. When I tell people about it—especially that it will revolve around a blog—I typically receive a sympathetic, concerned smile. A smile that tells me they think it's an appealing ambition, but also naïve.</h1>
            <p>
I often find myself procrastinating and postponing the act of sitting down to write. My mind is mostly on how much better the other writers are, and I feel hesitant to begin. Instead, I convince myself that it's more logical to finish designing my website first, or to read an article about how to write well.
<br><br>
This isn’t because I have nothing to say or because other people doubt me, but because I doubt myself. I’m still insecure in my abilities. I have to create my own plan, and I’m scared that I won’t be able to execute it.
<br><br>
I keep hoping that I’ll find some sort of guarantee that it’s all going to work out. Sure, I welcome good surprises and the occasional adventure, but for the most part, knowing what to expect lets me plan ahead and gives me some peace of mind.
<br><br>
As I’m contemplating my situation and my future, fear creeps in and tells me to reconsider. Maybe I should simply drop the idea. Maybe I should listen and choose the safer path. I’m tempted.
<br><br>
But before we decide to give in to peer pressure or to our insecure selves, it is helpful to see our feelings for what they are. When we are in doubt, understanding where our emotions and inclinations come from can enable us to make better, informed decisions.
<br><br>
As it turns out, our tendency to avoid the new and unfamiliar partly comes from our ancestors.
<br><br>    </p>
        </div>

        <div id="footer">
            <p> &#169; 2016 Peter Dam. All rights reserved.</p>
        </div>
</body>
</html>

答案 4 :(得分:-4)

创建 CustomUIViewController ,扩展 UIViewController 并覆盖 viewDidLoad ,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0];
}

之后,在控制器中使用CustomUIViewController。

Credits

相关问题