垂直alignmentUINavigationItems

我已经将UINavigationBar的高度自定义为100px,并且希望将button添加到此自定义栏上。

除了button似乎要坐在导航栏的底部,无论如何,一切都很好。 我不能让它alignment中心或导航栏的顶部。

在这里输入图像说明

这是代码; 首先我在应用程序委托中创build一个导航控制器,并在右侧添加一个button。

// set main navigation controller temp *tempc = [[temp alloc] initWithNibName:@"temp" bundle:nil]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:tempc]; UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]; [tempc.navigationItem setRightBarButtonItem:navItem]; [self.window addSubview:self.navigationController.view]; [self.window makeKeyAndVisible]; 

然后我调整“温度”视图控制器中的导航栏像这样:

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f); [self.navigationController.navigationBar setFrame:frame]; [self.navigationController.navigationBar setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin]; } 

我也尝试添加一个自定义视图到右边的ButtonButtonItem,但我无法得到添加的自定义视图完全触摸顶部。

在这里输入图像说明

而这个不幸的尝试的代码:

 // set main navigation controller temp *tempc = [[temp alloc] initWithNibName:@"temp" bundle:nil]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:tempc]; UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36.0f, 80.0f)]; [customView setBackgroundColor:[UIColor blueColor]]; UIButton *customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [customButton setFrame:CGRectMake(0, 22.0f, 36.0f, 36.0f)]; [customButton setTitle:@"+" forState:UIControlStateNormal]; [customView addSubview:customButton]; UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView]; 

有谁知道如何垂直alignmentUINavigationBar的UIBarButtonItems?

选项1:

  • 创buildUINavigationBar子类
  • 在这个类中覆盖- (void)layoutSubviews你将重新定位UIBarButtonItems
  • 在Interface Builder中将UINavigationBar类设置为UINavigationBar子类

例:

UINavigationBar的子类内部:

 - (void)layoutSubviews { int index = 0; for ( UIButton *button in [self subviews] ) { if(index == 1) { [button setFrame:CGRectMake(5,40,60,40)]; //leftBarButtonItem } else if(index == 2) { [button setFrame:CGRectMake(275,40,40,40)]; //rightBarButtonItem } ++index; } } 

视图控制器:

 - (void)viewDidLoad { ... UIBarButtonItem *navItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]; UIBarButtonItem *navItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil]; [self.navigationItem setRightBarButtonItem:navItem1]; [self.navigationItem setLeftBarButtonItem:navItem2]; CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f); [self.navigationController.navigationBar setFrame:frame]; ... } 

选项2(插入UIBarButtonItem不起作用):

 UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,100)]; [self.view addSubview:navigationBar]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(0,25,50,50)]; [navigationBar addSubview:button]; 

这会将页面标题和所有button向上移动20 px:

 [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault]; [[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault]; 

目前从alhcr(选项1)接受的答案不起作用,如果你只有一个rightBarButtonItem和没有离开。 这也是假设UINavigationBar的子视图顺序,这是不能保证,可以很好地改变在未来的iOS版本。 这是定制导航button框架的更好方法:

 // UINavigationBar subclass - (void)layoutSubviews { [super layoutSubviews]; UINavigationItem *navigationItem = [self topItem]; for (UIView *subview in [self subviews]) { if (subview == [[navigationItem rightBarButtonItem] customView]) { CGRect newRightButtonRect = CGRectMake(...new rect...); [subview setFrame:newRightButtonRect]; } else if (subview == [[navigationItem backBarButtonItem] customView]) { CGRect newBackButtonRect = CGRectMake(...new rect...); [subview setFrame:newBackButtonRect]; } } } 

通过在自定义button的图像边缘插入中进行调整,我发现了这个问题的解决scheme。 我有在应用程序的要求,以增加导航栏的高度,并增加高度后,使rightBarButtonItem和leftBarButtonItem图像未定位的问题。

find下面的代码: –

 UIImage *image = [[UIImage imageNamed:@"searchbar.png"]; UIButton* searchbutton = [UIButton buttonWithType:UIButtonTypeCustom]; [searchbutton addTarget:self action:@selector(searchBar:) forControlEvents:UIControlEventTouchUpInside]; searchbutton.frame = CGRectMake(0,0,22, 22); [searchbutton setImage:image forState:UIControlStateNormal]; [searchbutton setImageEdgeInsets:UIEdgeInsetsMake(-50, 0,50, 0)]; // Make BarButton Item UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchbutton]; self.navigationItem.rightBarButtonItem = navItem;