UINavigationController与自定义形状导航栏 [英] UINavigationController with custom shape navigation Bar

查看:87
本文介绍了UINavigationController与自定义形状导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义形状的自定义UINavigationBar,如下所示(忽略透明度)

I'm trying to create a custom UINavigationBar with a custom shape, like this (ignore transparency)

如您所见,此UINavigationBar具有自定义形状,我正在尝试复制它。

As you can see, this UINavigationBar has a custom shape and I'm trying to replicate it.

环顾四周我找到了这个回复,它解释了我遵循的第一步。

Looking around I found this response, where it explains the firsts steps I followed.

1)我创建了一个名为CustomNavigationBar的UINavigationBar的子类。
2)我像这样覆盖sizeThatFits方法:

1) I created a subclass of UINavigationBar called CustomNavigationBar. 2) I overrode the sizeThatFits method like this:

- (CGSize) sizeThatFits:(CGSize)size
{
  return CGSizeMake(320.0, 70.0);
}

3)这就是我失去的地方......

3) And here is where I'm lost...

在前面的答案中,可以使用UIBezierPath创建自定义形状(即使是曲线),然后应用为蒙版。我试过这个覆盖drawRect,但我得到的是一个很大的黑色导航栏(我的酒吧颜色设置为红色)。

In the previous answer is said that UIBezierPath can be used to create a custom shape (even with curves) and then applied as a mask. I tried this overriding drawRect but all i get is a big black navigation bar (my bar color is set to red).

编辑:我的抽签错了,这是正确的

- (void)drawRect:(CGRect)rect
{
  UIBezierPath *path = [[UIBezierPath alloc] init];
  [path moveToPoint:CGPointZero];
  [path addLineToPoint:CGPointMake(320.0, 0.0)];
  [path addLineToPoint:CGPointMake(320.0, 50.0)];
  [path addQuadCurveToPoint:CGPointMake(0.0, 50.0) controlPoint:CGPointMake(160.0, 90.0)];
  [path closePath];

  [[UIColor redColor] setFill];

  [path fill];
}

编辑:如下所述,我的代码有一些错误,现在它吸引了一些东西。

As said below, my code had some errors and now it draws something.

正如您所看到的,UIBezierPath正确定义了形状,但是存在一些新问题:

As you can see, the UIBezierPath defines the shape correctly but, there is some new problems:

1)状态栏是完全黑色的,没有任何东西在那里呈现,即使我将其颜色改为光,也没有显示任何内容。我缺少什么?

1) The status bar is completely black, nothing is rendered there, even if I change his color to light, it doesn't show nothing. What am I missing?

2)由于sizeThatFits方法,还剩下一些黑色部分。有没有办法让这部分透明?

2) Due to the sizeThatFits method, there's still some black part left. Is there any way to make that part transparent?

感谢所有人!

EDIT 2 :好吧,我完全改变了对这个问题的看法,我认为我已经接近解决方案了。现在我正在尝试使用透明的png文件作为背景,但仍然需要增加它的高度,所以现在这是我的代码。

EDIT 2: well, I totally changed my perspective about this issue and I think i'm getting close to a solution. Now I'm trying to use a transparent png file as background, but still need to increase it's height, so right now this is my code.

- (CGSize) sizeThatFits:(CGSize)size
{
   return [[UIImage imageNamed:@"Layer3"] size];
}

- (void)drawRect:(CGRect)rect
{
  [self setClipsToBounds:NO];
  UIImage *image = [UIImage imageNamed:@"Layer3"];
  [image drawInRect:rect];
}

更简单,对吧?显然,Layer3是我的透明png图像的名称。但现在,这就是我得到的。

Simpler ,right? Obviously "Layer3" is the name of my transparent png image. But now, this is what I'm getting.

如您所见,状态栏未包含在png图像中。

As you can see, the status bar is not covered with the png image.

我现在缺少什么?

谢谢!

推荐答案

嗯,我很傻xD

最后我找到了解决这个问题的方法,它比我简单得多预期。

Finally I got a solution for this issue and it was much simpler than I expected.

以下是步骤:

1)子类UINavigationBar,我创建了一个CustomNavigationBar对象。

1) Subclass UINavigationBar, I created a CustomNavigationBar object.

2)在它的初始化中,写下这行

2) Inside it's init, write this lines

UIImage *image = [UIImage imageNamed:@"barBackgroundImageName"];
[self setBackgroundImage:image forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
[self setShadowImage:[UIImage new]];

这很重要,也是我无法完成所有这些工作的原因, image's高度必须为64px 。我在在线Apple文档它描述了UINavigationBar和状态栏之间的行为。

This is important and the reason why I couldn't make all this thing work, image's height must be 64px. I find out this on the online Apple Documentation where it describes the behaviour between UINavigationBar and the Status Bar.

3)这就是全部。这是我的结果(我知道背景不对称,只是一个测试)。

3) And that's all. This is the result in my case (I know the background isn't symmetric, just a test).

感谢您的所有时间和帮助!

Thanks to all for your time and help!!!

这篇关于UINavigationController与自定义形状导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆