<i id='tbAdz'><tr id='tbAdz'><dt id='tbAdz'><q id='tbAdz'><span id='tbAdz'><b id='tbAdz'><form id='tbAdz'><ins id='tbAdz'></ins><ul id='tbAdz'></ul><sub id='tbAdz'></sub></form><legend id='tbAdz'></legend><bdo id='tbAdz'><pre id='tbAdz'><center id='tbAdz'></center></pre></bdo></b><th id='tbAdz'></th></span></q></dt></tr></i><div id='tbAdz'><tfoot id='tbAdz'></tfoot><dl id='tbAdz'><fieldset id='tbAdz'></fieldset></dl></div>

  • <legend id='tbAdz'><style id='tbAdz'><dir id='tbAdz'><q id='tbAdz'></q></dir></style></legend>
      <bdo id='tbAdz'></bdo><ul id='tbAdz'></ul>

      1. <small id='tbAdz'></small><noframes id='tbAdz'>

        <tfoot id='tbAdz'></tfoot>

        垂直对齐 UINavigationItems

        时间:2023-05-17
          <tbody id='Na98w'></tbody>
      2. <legend id='Na98w'><style id='Na98w'><dir id='Na98w'><q id='Na98w'></q></dir></style></legend>

        <small id='Na98w'></small><noframes id='Na98w'>

            • <tfoot id='Na98w'></tfoot>
              • <bdo id='Na98w'></bdo><ul id='Na98w'></ul>
                  <i id='Na98w'><tr id='Na98w'><dt id='Na98w'><q id='Na98w'><span id='Na98w'><b id='Na98w'><form id='Na98w'><ins id='Na98w'></ins><ul id='Na98w'></ul><sub id='Na98w'></sub></form><legend id='Na98w'></legend><bdo id='Na98w'><pre id='Na98w'><center id='Na98w'></center></pre></bdo></b><th id='Na98w'></th></span></q></dt></tr></i><div id='Na98w'><tfoot id='Na98w'></tfoot><dl id='Na98w'><fieldset id='Na98w'></fieldset></dl></div>

                1. 本文介绍了垂直对齐 UINavigationItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  I have customized the UINavigationBar's height to 100px and I'd like to add buttons onto this customized bar.

                  All is good except the button seems to want to sit on the bottom of the nav bar no matter what. I can't get it to align to the center or the top of the nav bar.

                  Here is the code; first I create a navigation controller in the app delegate and add one button on the right.

                  // 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];
                  

                  then I resize the navigation bar in the "temp" view controller like so:

                  - (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];
                  }
                  

                  I've also tried to add a custom view to the rightBarButtonItem but I can't get the added custom view to touch the top completely.

                  And the code for this unfortunate attempt:

                  // 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];
                  

                  Does anyone know how to vertically align UIBarButtonItems on a UINavigationBar?

                  解决方案

                  Option 1:

                  • create UINavigationBar subclass
                  • inside this class override - (void)layoutSubviews where you will reposition UIBarButtonItems
                  • in Interface Builder set UINavigationBar class to UINavigationBar subclass

                  Example:

                  inside subclass of 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;
                      }
                  }
                  

                  view controller:

                  - (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];
                  ...
                  }
                  

                  Option 2 (inserting UIBarButtonItem doesn't work):

                  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];
                  

                  这篇关于垂直对齐 UINavigationItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:“已加载笔尖,但未设置视图出口"例外 下一篇:UINavigationController-Alike for Desktop-Cocoa?

                  相关文章

                  <i id='IpLc4'><tr id='IpLc4'><dt id='IpLc4'><q id='IpLc4'><span id='IpLc4'><b id='IpLc4'><form id='IpLc4'><ins id='IpLc4'></ins><ul id='IpLc4'></ul><sub id='IpLc4'></sub></form><legend id='IpLc4'></legend><bdo id='IpLc4'><pre id='IpLc4'><center id='IpLc4'></center></pre></bdo></b><th id='IpLc4'></th></span></q></dt></tr></i><div id='IpLc4'><tfoot id='IpLc4'></tfoot><dl id='IpLc4'><fieldset id='IpLc4'></fieldset></dl></div>
                      <bdo id='IpLc4'></bdo><ul id='IpLc4'></ul>

                  1. <legend id='IpLc4'><style id='IpLc4'><dir id='IpLc4'><q id='IpLc4'></q></dir></style></legend>

                    <small id='IpLc4'></small><noframes id='IpLc4'>

                      <tfoot id='IpLc4'></tfoot>