Avi*_*ron 14 iphone objective-c uinavigationbar uibarbuttonitem ios
我创建了自己的UINavigationBar子类,以便启用高于44pxs的自定义背景.
我通过重写这两种方法来做到这一点:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
这就是结果:

现在,我可以看到的问题是所有UIBarButtonItem(和titleView)都放在导航栏的底部.
我希望它们固定在栏的顶部(当然有一些填充).为了达到这个目的,我需要覆盖什么?
谢谢!
编辑:
这是我使用的解决方案:
-(void) layoutSubviews
{
[super layoutSubviews];
for (UIView *view in self.subviews)
{
CGRect frame = view.frame;
frame.origin.y = 5;
view.frame = frame;
}
}
Run Code Online (Sandbox Code Playgroud)
对于空闲状态的技巧,在推送和弹出项目上仍然有一些奇怪的行为.
小智 5
要解决推/弹问题,请在sizeThatFits:(CGSize)大小中使用setTitleVerticalPositionAdjustment
- (CGSize)sizeThatFits:(CGSize)size {
UIImage *img = [UIImage imageNamed:@"navigation_background.png"];
[self setTitleVerticalPositionAdjustment:-7 forBarMetrics:UIBarMetricsDefault];
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize newSize = CGSizeMake(frame.size.width , img.size.height);
return newSize;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4705 次 |
| 最近记录: |