具有自定义高度的自定义UINavigationBar会导致UIBarButtonItem定位错误

编程入门 行业动态 更新时间:2024-10-11 15:22:42
本文介绍了具有自定义高度的自定义UINavigationBar会导致UIBarButtonItem定位错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了自己的UINavigationBar子类,以便启用高于44pxs的自定义背景。

I created my own subclass of UINavigationBar in order to enable custom background that is taller than 44pxs.

我是通过覆盖这两种方法来实现的:

I did it by overriding these two methods:

-(void) drawRect:(CGRect)rect { [self.backgroundImage drawInRect:CGRectMake(0, 0, self.backgroundImage.size.width, self.backgroundImage.size.height)]; } - (CGSize)sizeThatFits:(CGSize)size { CGRect frame = [UIScreen mainScreen].applicationFrame; CGSize newSize = CGSizeMake(frame.size.width , self.backgroundImage.size.height); return newSize; }

这就是结果:

现在,我可以看到的问题是所有UIBarButtonItem(和titleView)都放在导航栏的底部。

Now, my problem as you can see is that all the UIBarButtonItem's (and the titleView) are placed at the bottom of the navigation bar.

我希望它们是固定在栏的顶部(当然有一些填充)。 为了达到这个目的,我需要覆盖什么?

I would like them to be pinned to the top of the bar (with some padding of course). What to I need to override to achieve that?

谢谢!

编辑:

这是我使用的解决方案:

This is the solution that I used:

-(void) layoutSubviews { [super layoutSubviews]; for (UIView *view in self.subviews) { CGRect frame = view.frame; frame.origin.y = 5; view.frame = frame; } }

空闲状态的诀窍,还是有些奇怪推送和弹出项目的行为。

Does the trick for idle state, still have some weird behavior on push and pop items.

推荐答案

尝试覆盖 layoutSubviews :在里面调用 [super layoutSubviews] ,然后重新定位项目。

Try to override layoutSubviews: call [super layoutSubviews] inside and then reposition the items.

更多推荐

具有自定义高度的自定义UINavigationBar会导致UIBarButtonItem定位错误

本文发布于:2023-10-30 07:25:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   高度   错误   UINavigationBar   UIBarButtonItem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!