一句代码解决自定义NavigationItem的TitleView不居中

其实只用设置
navItem.titleView = titleLabel;
改为
self.navigationItem.titleView = titleLabel;
原因我也没去找,反正这样就可以用了.下面附上具体代码以供参考

pragma mark 設置狀態欄

-(void)setNavigationBar3{
// 创建一个导航栏
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];

//    //下面兩句加起來才能隐藏导航栏底部线
[navBar setShadowImage:[UIImage new]];
[navBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

// 导航栏背景颜色
//    navBar.barTintColor = [UIColor colorWithRed:101/255.0 green:215/255.0 blue:237/255.0 alpha:1.0];

//    navBar.barTintColor = [UIColor clearColor];
CGFloat labelwidth = 50 ;
// 自定义导航栏的title,用UILabel实现

// UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth/2-labelwidth/2, 0, labelwidth, 44)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, labelwidth, 44)];
titleLabel.text = @"登錄記錄";
titleLabel.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
titleLabel.font = [UIFont systemFontOfSize:18];
titleLabel.textAlignment = TextAlignmentCenter;
titleLabel.backgroundColor = [UIColor redColor];
// 创建导航栏组件
UINavigationItem *navItem = [[UINavigationItem alloc] init];

// // 设置自定义的title
// navItem.titleView = titleLabel;

//navItem.con
//navItem.titleView.
// 创建左侧按钮
// UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"leftButton" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick)];
// leftButton.tintColor = [UIColor purpleColor];
//
// // 创建右侧按钮
// UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"rightButton" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick)];
// rightButton.tintColor = [UIColor orangeColor];
//
// // 添加左侧、右侧按钮
// [navItem setLeftBarButtonItem:leftButton animated:false];
// [navItem setRightBarButtonItem:rightButton animated:false];
// 把导航栏组件加入导航栏
[navBar pushNavigationItem:navItem animated:false];

// 导航栏添加到view上
[self.view addSubview:navBar];

// UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 40, 44)];
[leftBtn addTarget:self action:@selector(enterInformationVC) forControlEvents:UIControlEventTouchUpInside];

[leftBtn setImage:[[UIImage imageNamed:@"backy"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

[leftBtn setImage:[[UIImage imageNamed:@"backy"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];

[leftBtn sizeToFit];

UILabel * leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
//    leftLabel.text = @"測試用戶";

// _usernameLabel = leftLabel;
leftLabel.textAlignment = TextAlignmentLeft;
leftLabel.textColor = [UIColor whiteColor];
UIBarButtonItem *laftLabelItem = [[UIBarButtonItem alloc] initWithCustomView:leftLabel];

UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];

leftLabel.userInteractionEnabled = YES;
UITapGestureRecognizer* singleTap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(enterInformationVC)];
[leftLabel addGestureRecognizer:singleTap2];


UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//按钮和文字的间隔距离
//         fixedSpaceBarButtonItem.width = 22;
fixedSpaceBarButtonItem.width = 0;

// UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 40, 44)];

[rightBtn addTarget:self action:@selector(logOutButtonClicked) forControlEvents:UIControlEventTouchUpInside];

[rightBtn setImage:[[UIImage imageNamed:@"news2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
[rightBtn setImage:[[UIImage imageNamed:@"newsh"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];
[rightBtn sizeToFit];
UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
CGFloat labelw = 15;
CGFloat labelh = 15;

UILabel * rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(rightBtn.bounds.size.width-labelw/2, rightBtn.frame.origin.y-labelh/2, labelw, labelh)];
rightLabel.text = @"1";
rightLabel.backgroundColor = [UIColor redColor];

// _logoutLabel = rightLabel;
rightLabel.font = [UIFont systemFontOfSize:12];
rightLabel.textColor = [UIColor whiteColor];
rightLabel.textAlignment = TextAlignmentCenter;
[rightBtn addSubview:rightLabel];

//设置圆角
{
    UIBezierPath * path3 = [UIBezierPath bezierPathWithRoundedRect:rightLabel.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight|UIRectCornerTopLeft|UIRectCornerTopRight  cornerRadii:CGSizeMake(labelh/2, labelh/2)];
    CAShapeLayer * layer3 = [[CAShapeLayer alloc]init];
    layer3.frame = rightLabel.bounds;
    layer3.path = path3.CGPath;
    rightLabel.layer.mask = layer3;
}

//    UIBarButtonItem *rightLabelItem = [[UIBarButtonItem alloc] initWithCustomView:rightLabel];

rightLabel.userInteractionEnabled = YES;
UITapGestureRecognizer* singleTap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(logOutButtonClicked)];
[rightLabel addGestureRecognizer:singleTap1];
//这样添加不上右边的按钮
//    navItem.leftBarButtonItems  = @[leftBtnItem,laftLabelItem];
//     navItem.rightBarButtonItem = rightBtnItem;
//可以添加上两边的按钮

// self.navigationItem.leftBarButtonItems = @[leftBtnItem];
// self.navigationItem.rightBarButtonItems = @[rightBtnItem];

 self.navigationItem.leftBarButtonItem  = leftBtnItem;

// self.navigationItem.rightBarButtonItem = rightBtnItem;

// 设置自定义的title
self.navigationItem.titleView = titleLabel;

// self.navigationItem.rightBarButtonItem.hi
// [self.navigationItem.titleView layoutIfNeeded];

}

你可能感兴趣的:(一句代码解决自定义NavigationItem的TitleView不居中)