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

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

      1. 以编程方式快速更改导航栏的高度

        Programmatically change height of navigation bar in swift(以编程方式快速更改导航栏的高度)
        <legend id='0JgLK'><style id='0JgLK'><dir id='0JgLK'><q id='0JgLK'></q></dir></style></legend>

          <tbody id='0JgLK'></tbody>

            • <small id='0JgLK'></small><noframes id='0JgLK'>

                <bdo id='0JgLK'></bdo><ul id='0JgLK'></ul>

                1. <tfoot id='0JgLK'></tfoot>

                2. <i id='0JgLK'><tr id='0JgLK'><dt id='0JgLK'><q id='0JgLK'><span id='0JgLK'><b id='0JgLK'><form id='0JgLK'><ins id='0JgLK'></ins><ul id='0JgLK'></ul><sub id='0JgLK'></sub></form><legend id='0JgLK'></legend><bdo id='0JgLK'><pre id='0JgLK'><center id='0JgLK'></center></pre></bdo></b><th id='0JgLK'></th></span></q></dt></tr></i><div id='0JgLK'><tfoot id='0JgLK'></tfoot><dl id='0JgLK'><fieldset id='0JgLK'></fieldset></dl></div>
                3. 本文介绍了以编程方式快速更改导航栏的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想制作一个更高的导航栏,我正在尝试使用 UINavigationBar 的子类来做到这一点.

                  I want to make a taller navigation bar and I am trying to do this using a subclass of UINavigationBar.

                  这是我的 UINavigationBar 的子类:

                  Here is my subclass of UINavigationBar:

                  import UIKit
                  class TallerNaviBar: UINavigationBar {
                      override func sizeThatFits(size: CGSize) -> CGSize {
                          var newSize:CGSize = CGSizeMake(size.width, 87)
                          return newSize
                      }
                  }
                  

                  在我嵌入导航控制器的 ViewController 中,我在 viewDidLoad() 函数中编写了以下代码

                  And in my ViewController which is embedded in a navigation controller, I write the following code in the viewDidLoad() function

                  self.navigationController!.setValue(TallerNaviBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 87)), forKeyPath: "navigationBar")
                  

                  没有报告错误,但运行代码时我什么也没得到.一个完全空白的视图..

                  There is no error reported, but I get nothing when I run the code. A completely blank view. .

                  有什么建议吗?或者其他改变高度的方法?谢谢.

                  Any suggestion? Or some other way of changing the height? Thanks.

                  推荐答案

                  iOS 11 及更高版本更新

                  苹果不想让你弄乱导航栏的高度,所以不要碰它见这里:https://openradar.appspot.com/32912789在这里:https://forums.developer.apple.com/thread/88202#274620

                  apple doesn't want you messing with the navigation bar height, so don't touch it see here: https://openradar.appspot.com/32912789 and here: https://forums.developer.apple.com/thread/88202#274620

                  class TallerNaviBar: UINavigationBar {
                      override func sizeThatFits(size: CGSize) -> CGSize {
                          var newSize:CGSize = CGSizeMake(self.superview!.frame.size.width, 87)
                          return newSize
                      }
                  }
                  

                  我不会快速,但答案很简单,这里是 ObjC

                  I don't do swift, but the answer is easy, here's ObjC

                  - (CGSize)sizeThatFits:(CGSize)size {
                  
                           return CGSizeMake([self superview].frame.size.width, 40);
                  
                  }
                  

                  这是我对 Swift 的解释:

                  Here's my interpretation in Swift:

                  import UIKit
                  class TallerNaviBar: UINavigationBar {
                      override func sizeThatFits(size: CGSize) -> CGSize {
                          var newSize:CGSize = CGSizeMake(superview.width, 87)
                          return newSize
                      }
                  }
                  

                  你会遇到的问题不是这个方法,这是简单的部分,问题是强制导航控制器总是使用这个导航栏

                  The problem you will have isn't with this method, this is the easy part, the problem is forcing the navigation controller to always use this navigation bar

                  我对 IOS 中的所有内容进行子类化和调整大小,包括导航控制器和 tabbarcontrollers,为了强制所有导航控制器使用此导航栏,您必须子类化一个 navigationController,并且仅在整个应用程序中使用此导航控制器,这是子类的工作方式,这是 Obj C,所以你必须翻译它:

                  I subclass and resize everything in IOS, including navigation controllers and tabbarcontrollers, in order to enforce that all navigation controllers use this navigation bar, you must subclass a navigationController and only use this navigationcontroller throughout your app, here's how the subclass works, this is Obj C, so you'll have to translate it:

                  @interface NSHNavigationController () <UINavigationBarDelegate, UINavigationControllerDelegate>
                  {
                  
                  }
                  
                  @implementation NSHNavigationController
                  
                  #pragma mark Initialization
                  
                  - (id)initWithCoder:(NSCoder *)aDecoder
                  {
                      self = [super initWithCoder:aDecoder];
                      if (self) {
                          [self NSHSetupNavigationController];
                      }
                      return self;
                  }
                  
                  - (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass
                  {
                      self = [super initWithNavigationBarClass:navigationBarClass toolbarClass:toolbarClass];
                      if (self) {
                          [self NSHSetupNavigationController];
                      }
                      return self;
                  }
                  
                  - (id)initWithRootViewController:(UIViewController *)rootViewController
                  {
                      self = [super initWithRootViewController:rootViewController];
                      if (self) {
                          [self NSHSetupNavigationController];
                      }
                      return self;
                  }
                  
                  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
                  {
                      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
                      if (self) {
                          [self NSHSetupNavigationController];
                      }
                      return self;
                  }
                  
                  - (void)dealloc
                  {
                      [self setInternalDelegate:nil];
                      [self setExternalDelegate:nil];
                  }
                  
                  #pragma mark Setup
                  
                  - (void)NSHSetupNavigationController
                  {
                      [self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];
                  }
                  

                  这条线将为您完成:

                     [self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];
                  

                  哦,是的,请确保您将导航栏子类化,您说过您是,但您是这样做的,很简单:

                  Oh yeah, and make sure you are subclassing the nav bar, you said you were, but here's how you do it, it's simple:

                  #import "NSHNavigationBar.h"
                  
                  @implementation NSHNavigationBar
                  - (id)initWithFrame:(CGRect)frame {
                      self = [super initWithFrame:frame];
                      if (self) {
                          NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                                       NSFontAttributeName:fontMagicForRegularNSHFont};
                          [self setTitleTextAttributes:attributes];
                          [self setTranslucent:true];
                          [self setBackgroundColor:[UIColor clearColor]];
                      }
                      return self;
                  }
                  - (CGSize)sizeThatFits:(CGSize)size {
                  
                      return CGSizeMake([self superview].frame.size.width, heightResizer(40));
                  
                  }
                  
                  - (void)layoutSubviews {
                  
                      [super layoutSubviews];
                  
                  }
                  

                  因此,总而言之,将 UINavigationBar 和 UINavigationController 子类化并设置好,这将允许您随时操作导航栏,您也可以键入转换视图控制器的导航栏,这有点疯了,会迷惑很多人,但这里是这样的:

                  So, in summary, subclass both the UINavigationBar and the UINavigationController and you are set, this will allow you to manipulate the navigation bar whenever you'd like, you can also type cast your view controller's navigation bars, this is a little nuts and will confuse a lot of people, but here it goes:

                  -(CustomNavigationBar *)navBar {
                  
                      return (id)[self.navigationController navigationBar];
                  }
                  

                  把上面的东西放在你的视图控制器中,然后你这样称呼它:

                  put the stuff above in your view controller and then you call it like this:

                  [[self navBar] setBackGroundColor:[UIColor blueColor]];
                  

                  这将成功地将您的导航控制器类型转换为您的自定义导航栏,如果您想从这里更改导航栏的高度,那么您可以执行以下操作:

                  This will successfully typecast your navigationcontroller to be your custom navigation bar, if you want to change the height of the nav bar from here, then you can do something like this:

                  这篇关于以编程方式快速更改导航栏的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  UINavigationController inside a UITabBarController inside a UISplitViewController presented modally on iPhone(UISplitViewController 内的 UITabBarController 内的 UINavigationController 以模态方式呈现在 iPhone 上) - IT屋-程序员软件开发技术分
                  ViewController in UINavigationController orientation change(UINavigationController 中的 ViewController 方向更改)
                  Custom back button in UINavigationController(UINavigationController 中的自定义后退按钮)
                  How to add a navigation controller programmatically in code but not as initial view controller(如何在代码中以编程方式添加导航控制器,但不作为初始视图控制器)
                  The correct way to set a light status bar text color in iOS 7 based on different ViewControllers(iOS 7中基于不同ViewControllers设置灯光状态栏文字颜色的正确方法)
                  View being blocked by UITransitionView after being presented(呈现后被 UITransitionView 阻止的视图)

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

                  • <bdo id='X9fIy'></bdo><ul id='X9fIy'></ul>
                    1. <legend id='X9fIy'><style id='X9fIy'><dir id='X9fIy'><q id='X9fIy'></q></dir></style></legend>

                            <tbody id='X9fIy'></tbody>
                          <tfoot id='X9fIy'></tfoot>

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