Aid*_*den 3 uinavigationbar uinavigationcontroller ios swift
我想创建一个更高的导航栏,我想尝试使用UINavigationBar的子类.
这是我的UINavigationBar的子类:
import UIKit
class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(size.width, 87)
return newSize
}
}
Run Code Online (Sandbox Code Playgroud)
在嵌入导航控制器的ViewController中,我在viewDidLoad()函数中编写以下代码
self.navigationController!.setValue(TallerNaviBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 87)), forKeyPath: "navigationBar")
Run Code Online (Sandbox Code Playgroud)
报告没有错误,但是在运行代码时我什么都没得到.一个完全空白的视图.
.
有什么建议吗?或者其他一些改变高度的方法?谢谢.
适用于iOS 11及更高版本的更新
苹果不希望你搞乱导航栏高度,所以不要触摸它看到这里:https://openradar.appspot.com/32912789 和这里: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
}
}
Run Code Online (Sandbox Code Playgroud)
我不急,但答案很简单,这是ObjC
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake([self superview].frame.size.width, 40);
}
Run Code Online (Sandbox Code Playgroud)
这是我在Swift中的解释:
import UIKit
class TallerNaviBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
var newSize:CGSize = CGSizeMake(superview.width, 87)
return newSize
}
}
Run Code Online (Sandbox Code Playgroud)
你将遇到的问题不是这个方法,这是容易的部分,问题是迫使导航控制器总是使用这个导航栏
我将IOS中的所有内容(包括导航控制器和tabbarcontrollers)子类化并调整大小,为了强制所有导航控制器使用此导航栏,您必须子类化navigationController并且只在整个应用程序中使用此导航控制器,这是子类的工作方式,这是Obj C,所以你必须翻译它:
@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"];
}
Run Code Online (Sandbox Code Playgroud)
这是为你做的那条线:
[self setValue:[[NSHNavigationBar alloc]init] forKeyPath:@"navigationBar"];
Run Code Online (Sandbox Code Playgroud)
哦,是的,并确保你是导航栏的子类,你说你是,但这是你如何做到这一点,这很简单:
#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];
}
Run Code Online (Sandbox Code Playgroud)
所以,总而言之,你设置UINavigationBar和UINavigationController的子类,这将允许你随时操作导航栏,你也可以输入你的视图控制器的导航栏,这是一个小坚果和意志混淆了很多人,但在这里:
-(CustomNavigationBar *)navBar {
return (id)[self.navigationController navigationBar];
}
Run Code Online (Sandbox Code Playgroud)
把上面的东西放在你的视图控制器中,然后你这样调用它:
[[self navBar] setBackGroundColor:[UIColor blueColor]];
Run Code Online (Sandbox Code Playgroud)
这将成功将您的导航控制器强制转换为自定义导航栏,如果您想从此处更改导航栏的高度,那么您可以执行以下操作:
小智 8
这是一个基于最小子类的简单Swift 3解决方案.第一个子类UINavigationBar.:
class CustomNavigationBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
let newSize :CGSize = CGSize(width: self.frame.size.width, height: 54)
return newSize
}
}
Run Code Online (Sandbox Code Playgroud)
在代码中创建导航控制器并使用带有自定义导航栏类的初始化程序.
let nav = UINavigationController(navigationBarClass: CustomNavigationBar.self,
toolbarClass: nil)
Run Code Online (Sandbox Code Playgroud)
将保留UINavigationBar的所有现有行为,并采用您的自定义高度.
| 归档时间: |
|
| 查看次数: |
13073 次 |
| 最近记录: |