我将UINavigationBar子类化,并根据设备更改其高度。一切都很完美,直到我添加按钮。如果默认高度发生变化,按钮不会位于中央,而是位于底部。
#import "RevUINavigationBar.h"
#import "Utils.h"
#define MAIN_COLOR_COMPONENTS { 0, 0, 0, 1.0, 0, 0, 0, 1.0 }
#define LIGHT_COLOR_COMPONENTS { 0.263, 0.263, 0.263, 1.0, 0.263, 0.263, 0.263, 1.0 }
@implementation RevUINavigationBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// emulate the tint colored bar
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat locations[2] = { 0.0, 1.0 };
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGFloat topComponents[8] = MAIN_COLOR_COMPONENTS;
CGGradientRef topGradient = CGGradientCreateWithColorComponents(myColorspace, topComponents, locations, 2);
CGContextDrawLinearGradient(context, topGradient, CGPointMake(0, 0), CGPointMake(0, self.frame.size.height/2), 0);
CGGradientRelease(topGradient);
CGFloat botComponents[8] = LIGHT_COLOR_COMPONENTS;
CGGradientRef botGradient = CGGradientCreateWithColorComponents(myColorspace, botComponents, locations, 2);
CGContextDrawLinearGradient(context, botGradient,
CGPointMake(0, self.frame.size.height/2), CGPointMake(0, self.frame.size.height), 0);
CGGradientRelease(botGradient);
CGColorSpaceRelease(myColorspace);
// top Line
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, self.frame.size.width, 0);
CGContextStrokePath(context);
// bottom line
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0);
CGContextMoveToPoint(context, 0, self.frame.size.height);
CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
CGContextStrokePath(context);
}
- (CGSize)sizeThatFits:(CGSize)size
{
int navigationBarHeight = 44;
if ([Utils isIpad] == YES)
navigationBarHeight = 88;
return CGSizeMake(self.frame.size.width, navigationBarHeight);
}
@end
添加按钮:
- (void)showCustomNavBarItems
{
NSMutableArray *leftItems = [NSMutableArray new];
if (self != [[self.navigationController viewControllers] objectAtIndex:0])
{
UIImage *backBtnImg = [UIImage imageNamedSmart:@"BackB"];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn addTarget:self action:@selector(backBarButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[backBtn setImage:backBtnImg forState:UIControlStateNormal];
backBtn.frame = CGRectMake(0, 0, backBtnImg.size.width, backBtnImg.size.height);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
backButton.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[leftItems addObject:backButton];
}
UIImageView *logoView = [[UIImageView alloc]initWithImage:[UIImage imageNamedSmart:@"Logo"]];
UIBarButtonItem *logo = [[UIBarButtonItem alloc] initWithCustomView:logoView];
[leftItems addObject:logo];
self.navigationItem.leftBarButtonItems = leftItems;
[self.navigationController.navigationBar layoutSubviews];
}
答案 0 :(得分:0)
解决方案是覆盖函数layoutSubViews并将我的所有控件设置到正确的位置:
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *view in self.subviews)
view.frame = CGRectMake(view.frame.origin.x,
self.frame.size.width / 2 - view.frame.size.width / 2,
view.frame.size.width,
view.frame.size.height);
}