UITabBarController+UINavigationController+UIToolBar

Posted 山水间

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UITabBarController+UINavigationController+UIToolBar相关的知识,希望对你有一定的参考价值。

一,各种名称和位置

//技巧一键换肤
    [[UINavigationBar appearance]setBarTintColor:[UIColor redColor]];//给所有的NavigationBar换颜色
    [[UITabBar appearance]setBarTintColor:[UIColor blueColor]];//给所有的TabBar换颜色

二,UINavigationBar:

1,设置导航条透明度:self.navigationBar.translucent = NO;

2,设置Bar背景颜色:self.navigationBar.barTintColor = [UIColour RedColour];->The tint color to apply to the navigation bar background.修改bar的背景颜色

3,设置navigation items和bar button items颜色:self.navigationBar.tintColour = [UIColour BlurColour];->The tint color to apply to the navigation items and bar button items.修改navigation items和bar button items;

4,设置返回箭头的自定义图片//imageWithRenderingMode 图片的显示模式,要设置成originnal图片的颜色才会变成自定义的图片颜色,否则默认系统颜色。

UIImage *backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//注意下面两个属性都要设置,只设置一个是无效的。

[UINavigationBar appearance]setBackIndicatorTransitionMaskImage:backimage];

[UINavigationBar appearance]setBackIndicatorImage:backimage];

5,去掉导航栏的文字,钻了空子,设置了文字的PositionAdjustment就可以了:

UIBarButtonItem baritem = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class],nil];

UIOffset offset;

offset.horizontal = -500;

[baritem setBackButtonTitlePositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];

typedef NS_ENUM(NSInteger, UIBarMetrics) {
    UIBarMetricsDefault,//正常竖屏状态
    UIBarMetricsCompact,//横屏状态
};

 

6,导航栏文字的设置.

//返回按钮字体属性

[baritem setTitleTextAttributes:@{NSFontAttributeColor:[UIColor RedColor]} forState:UIControlStateNormal];

//改变导航的title样式

NSDictionary *TitleAttributes = [NSDictonary dictionaryWithObjectsAndKeys:Nav_title_Colour,NSForegroundcolourAttributeName,Nav_Titlefont,NSfontAttributeName,nil];

[[UINavigationBar appearance]setTitleTextAttributes:titleAttributes];

Title也是一个Item,TitleView可以将Title换为一个UIView,Title上面可以添加文字:

eg:

- (void)viewDidLoad {
    [super viewDidLoad];
    UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"Title1"];
    UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 64)];
    [[UINavigationBar appearance]setBarTintColor:[UIColor greenColor]];
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    titleView.backgroundColor = [UIColor redColor];
    item.titleView = titleView;
    item.prompt = @"标题的介绍";
    //隐藏返回按钮:hidesBackButton
    
    [bar pushNavigationItem:item animated:YES];
    [self.view addSubview:bar];
    self.view.backgroundColor = [UIColor grayColor];
}

 

左右侧的返回按钮可以自己添加设置:

@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
- (void)setLeftBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
- (void)setRightBarButtonItem:(nullable UIBarButtonItem *)item animated:(BOOL)animated;
//添加多个按钮:
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *leftBarButtonItems;
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *rightBarButtonItems;
- (void)setLeftBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated;
- (void)setRightBarButtonItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated;

 ButtonItemStyle:

typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) {
    UIBarButtonItemStylePlain,
    UIBarButtonItemStyleDone,
};

 

也可以添加图片创建ButtonItem:

- (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
- (instancetype)initWithImage:(nullable UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;//landscapeImagePhone style这个参数可以设置设备横屏时的图片

//自定义View:
- (instancetype)initWithCustomView:(UIView *)customView;

//原生的BarButtonItem:
UIBarButtonItem * button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];

typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {
    UIBarButtonSystemItemDone,//显示完成
    UIBarButtonSystemItemCancel,//显示取消
    UIBarButtonSystemItemEdit,  //显示编辑
    UIBarButtonSystemItemSave, //显示保存 
    UIBarButtonSystemItemAdd,//显示加号
    UIBarButtonSystemItemFlexibleSpace,//什么都不显示,占位一个空间位置
    UIBarButtonSystemItemFixedSpace,//和上一个类似
    UIBarButtonSystemItemCompose,//显示写入按钮
    UIBarButtonSystemItemReply,//显示循环按钮
    UIBarButtonSystemItemAction,//显示活动按钮
    UIBarButtonSystemItemOrganize,//显示组合按钮
    UIBarButtonSystemItemBookmarks,//显示图书按钮
    UIBarButtonSystemItemSearch,//显示查找按钮
    UIBarButtonSystemItemRefresh,//显示刷新按钮
    UIBarButtonSystemItemStop,//显示停止按钮
    UIBarButtonSystemItemCamera,//显示相机按钮
    UIBarButtonSystemItemTrash,//显示移除按钮
    UIBarButtonSystemItemPlay,//显示播放按钮
    UIBarButtonSystemItemPause,//显示暂停按钮
    UIBarButtonSystemItemRewind,//显示退后按钮
    UIBarButtonSystemItemFastForward,//显示前进按钮
    UIBarButtonSystemItemUndo,//显示消除按钮
    UIBarButtonSystemItemRedo ,//显示重做按钮
    UIBarButtonSystemItemPageCurl ,//在tool上有效
};

 

 

 7,  UINavigationBar上面不只是简单的显示标题,它也将标题进行了堆栈的管理,每一个标题抽象为的对象在ios系统中是UINavigationItem对象,我们可以通过push与pop操作管理item组。

//向栈中添加一个item,上一个item会被推向导航栏的左侧,变为pop按钮,会有一个动画效果
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//pop一个item
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; 
//当前push到最上层的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//仅次于最上层的item,一般式被推向导航栏左侧的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//获取堆栈中所有item的数组
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//设置一组item
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;

 8,从一个导航条跳转到另一个导航条,重写系统的Push到下一个页面的方法。

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{

  if(self.viewController.count > 0){

    //第二层viewController 隐藏tabbar

    ViewController.hidesBottomBarWhenPushed = Yes;

  }

  [super pushViewController:viewController animated:YES];

}

8,修改导航条下面黑线的颜色

[self.navigationController.navigationBar setShadowImage:[UIimage imageWithName:imagecolour];

9,UINavigationBarDelegate

@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;

 通过代理,我们可以监控导航栏的一些push与pop操作:

//item将要push的时候调用,返回NO,则不能push
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item; 
//item已经push后调用
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item; 
//item将要pop时调用,返回NO,不能pop  
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item; 
//item已经pop后调用 
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

 

三,UITabBarController

1,设置背景颜色:self.tabbar.barTintColor = Tab_bar_backcolor;

2,设置标签不透明:self.tabbar.translucent = NO;

3,常规设置:

NSArray classNameArray = [NSArray arrayWithObject:@"firstViewcontroller",@"SecondViewController"@"ThirdViewController",nil];

NSArray titleArray = [NSArray arrayWithObject:@"first",@"Second",@"Third",nil];

NSArray normalImageArray = [NSArray arrayWIthObjects:@"first_normal.png",@"second_normal.png",@"third_normal.png",nil];

NSArray SelectedImageArray = [NSArray arrayWIthObjects:@"first_Selected.png",@"second_Selected.png",@"third_Selected.png",nil];

//添加到Tabbar上

NSMutableArray *navigationArray = [NSMutableArray alloc]init];

for(int i = 0;i<classNameArray.count;i++){

  UIViewController *vc = (UIViewController *)[[NSClassFromString(classNameArray[i])alloc]init;

  vc.title = titleArray[i];

  UIimage *normalImage = [UIImage imageNamed:normalImageArray[i]];

  UIimage *selectedImage = [uiImage imageNamed:selectedImageArray[i]];

  vc.tabBarItem.image =  [normal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

  vc.taBarItem.selectedImage = [slelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

}

//去掉标签栏的文字,设置偏移值:

UIOffet offset = [UIOffetMake(0,300);

[vc.tabBarItem setTitlePositionAdjustment:offset];

BaseNavigationViewController na = [[BaseNavigationViewController alloc]initWithRootViewController:vc];

[navigationArray addobject:na];

}

self.viewController = navigationArray;

//标签栏文字设置

UIBaritem item = [UIBarItem appearance];

//正常状态的文字

[item setTitleAttributes:@{NSForegroundColorAttributeName:Tab_color,NSfontAttributeName:Tab-Font} forState:UIControlStateNormal];

//选择的文字

[item setTitleAttributes:@{NSForegroundColorAttributeName:Tab_color_Selected,NSfontAttributeName:Tab-Font_Selected} forState:UIControlStateSelected];

5,修改标签栏下面黑线的颜色

[self.tabBarController.tabbar setShadowImage:[UIimage imageWithName:imagecolour];

 

三,UIToolBar:(UITabbar的位置,工具条)

 UIToolBar一般放在下面:

//工具栏的风格,和导航栏类似,有黑白两种
@property(nonatomic) UIBarStyle barStyle; 
//设置工具栏上按钮数组
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *items; 
//设置工具栏是否透明
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent; 
//设置工具栏按钮
- (void)setItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated; 
//设置item风格颜色
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
//设置工具栏背景色
@property(nullable, nonatomic,strong) UIColor *barTintColor;
//设置工具栏背景和阴影图案
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
- (nullable UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
- (void)setShadowImage:(nullable UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom;
- (nullable UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom;

 

 

以上是关于UITabBarController+UINavigationController+UIToolBar的主要内容,如果未能解决你的问题,请参考以下文章

UITabBarController中的UITableViewController = iOS 11上不需要的填充

当我在 iOS 4.3 中调用 ViewDidAppear 时,它不起作用?

从一个 UITabBarController 导航到另一个 UITabBarController

UITabBarController

如何使屏幕仅垂直。 (UITabBarController/Swift)

UITabBarController简单介绍