问题描述
我创建了自己的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定位错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!