我想让导航视图的顶部变小一些.你会如何实现这一目标?这是我到目前为止所尝试过的,但正如你所看到的,即使我使导航栏变小,它曾经占据的区域仍然存在(黑色).
[window addSubview:[navigationController view]]; navigationController.view.frame = CGRectMake(0, 100, 320, 280); navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 20); navigationController.view.backgroundColor = [UIColor blackColor]; [window makeKeyAndVisible];
使用自定义sizeThatFits创建UINavigationBar类别.
@implementation UINavigationBar (customNav) - (CGSize)sizeThatFits:(CGSize)size { CGSize newSize = CGSizeMake(self.frame.size.width,70); return newSize; } @end
使用此导航栏子类,我已成功在iPad上的iOS 5.x到iOS 6.x上创建了一个更大的导航栏.这给了我一个更大的导航栏,但没有打破所有的动画.
static CGFloat const CustomNavigationBarHeight = 62; static CGFloat const NavigationBarHeight = 44; static CGFloat const CustomNavigationBarHeightDelta = CustomNavigationBarHeight - NavigationBarHeight; @implementation HINavigationBar - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // UIColor *titleColor = [[HITheme currentTheme] fontColorForLabelForLocation:HIThemeLabelNavigationTitle]; // UIFont *titleFont = [[HITheme currentTheme] fontForLabelForLocation:HIThemeLabelNavigationTitle]; // [self setTitleTextAttributes:@{ UITextAttributeFont : titleFont, UITextAttributeTextColor : titleColor }]; CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -CustomNavigationBarHeightDelta / 2.0); self.transform = translate; [self resetBackgroundImageFrame]; } return self; } - (void)resetBackgroundImageFrame { for (UIView *view in self.subviews) { if ([NSStringFromClass([view class]) rangeOfString:@"BarBackground"].length != 0) { view.frame = CGRectMake(0, CustomNavigationBarHeightDelta / 2.0, self.bounds.size.width, self.bounds.size.height); } } } - (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics { [super setBackgroundImage:backgroundImage forBarMetrics:barMetrics]; [self resetBackgroundImageFrame]; } - (CGSize)sizeThatFits:(CGSize)size { size.width = self.frame.size.width; size.height = CustomNavigationBarHeight; return size; } - (void)setFrame:(CGRect)frame { [super setFrame:frame]; [self resetBackgroundImageFrame]; } @end
对于迅捷
创建Uinavigation栏的子类.
import UIKit class higherNavBar: UINavigationBar { override func sizeThatFits(size: CGSize) -> CGSize { var newSize:CGSize = CGSizeMake(self.frame.size.width, 87) return newSize }
两侧将有两个空白条,我将宽度更改为确切的数字,以使其工作.
但是标题和后退按钮与底部对齐.
没有必要继承UINavigationBar.在Objective-C中,您可以使用类别,在Swift中,您可以使用扩展名.
extension UINavigationBar { public override func sizeThatFits(size: CGSize) -> CGSize { return CGSize(width: frame.width, height: 70) } }