对于想了解swiftTabBar的读者,本文将是一篇不可错过的文章,我们将详细介绍swifttabbar颜色选择器,并且为您提供关于FlutterTabBar、flutterTabBar底部导航栏、F
对于想了解swift TabBar的读者,本文将是一篇不可错过的文章,我们将详细介绍swift tabbar颜色选择器,并且为您提供关于Flutter TabBar、flutter TabBar 底部导航栏、Flutter TabBar 标签栏背景颜色、点击水波纹颜色配置、How to create tabbar with ESTabBarController in swift 3.0的有价值信息。
本文目录一览:- swift TabBar(swift tabbar颜色选择器)
- Flutter TabBar
- flutter TabBar 底部导航栏
- Flutter TabBar 标签栏背景颜色、点击水波纹颜色配置
- How to create tabbar with ESTabBarController in swift 3.0
swift TabBar(swift tabbar颜色选择器)
先上效果图如下:
1. 在APP 的AppDelegate页面中加载TFTabBarViewController类
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame:UIScreen.mainScreen().bounds) //let rootController = RootViewController(style: UITableViewStyle.Plain) let rootController = TFTabBarViewController() //TFLoginViewController() let rootNav = UINavigationController(rootViewController: rootController) self.window!.rootViewController = rootNav self.window!.backgroundColor = UIColor.whiteColor() self.window!.makeKeyAndVisible() return true }
2. 创建 FirstViewController ,SecondViewController,ThirdViewController,FourthViewController四个页面,然后设置下页面的颜色(便于点击下面的TabBar时,可以看到切换的效果)
3. 将刚刚创建的页面加载到TabBar中去
以下是源码:
import UIKit class TFTabBarViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() self.loadTabBarViewControllers() } func loadTabBarViewControllers(){ let firstVC = FirstViewController () let item1 : UITabBarItem = UITabBarItem (title: "首页",image: UIImage(named: "home_normal"),selectedImage: UIImage(named: "home_highlight")) firstVC.tabBarItem = item1 let secondVC = SecondViewController () let item2 : UITabBarItem = UITabBarItem (title: "购物",image: UIImage(named: "message_normal"),selectedImage: UIImage(named: "message_highlight")) secondVC.tabBarItem = item2 let thirdVC = ThirdViewController () let item3 : UITabBarItem = UITabBarItem (title: "旅游",image: UIImage(named: "mycity_normal"),selectedImage: UIImage(named: "mycity_highlight")) thirdVC.tabBarItem = item3 let fourthVC = FourthViewController () let item4 : UITabBarItem = UITabBarItem (title: "旅游",image: UIImage(named: "account_normal"),selectedImage: UIImage(named: "account_highlight")) fourthVC.tabBarItem = item4 let tabArray = [firstVC,secondVC,thirdVC,fourthVC] self.viewControllers = tabArray } }
Flutter TabBar
先看一下Tab的构造方法:
TabBar({
Key key,
@required this.tabs,
this.controller,
this.isScrollable: false,
this.indicatorColor,
this.indicatorWeight: 2.0,
this.indicatorPadding: EdgeInsets.zero,
this.indicator,
this.indicatorSize,
this.labelColor,
this.labelStyle,
this.unselectedLabelColor,
this.unselectedLabelStyle,
})
Tab使用方法
import ''package:flutter/material.dart'';
class MyHomePage extends StatefulWidget {
@override
createState() => new MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
final List<Tab> myTabs = <Tab>[
Tab(text: ''明教''),
Tab(text: ''霸刀''),
Tab(text: ''天策''),
Tab(text: ''纯阳''),
Tab(text: ''少林''),
Tab(text: ''藏剑''),
Tab(text: ''七秀''),
Tab(text: ''五毒''),
];
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: myTabs.length,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: TabBar(
tabs: myTabs,
isScrollable: true,
indicatorColor: Colors.red,
labelColor: Colors.white,
),
),
body: TabBarView(
children: myTabs
.map((Tab tab) => Center(child: Text(tab.text)))
.toList()),
),
);
}
}
Flutter切换tab后保留tab状态
Flutter中为了节约内存不会保存widget的状态,widget都是临时变量。当我们使用TabBar,TabBarView是我们就会发现,切换tab后再重新切换回上一页面,这时候tab会重新加载重新创建,体验很不友好。Flutter出于自己的设计考虑并没有延续android的ViewPager这样的缓存页面设计,毕竟控件两端都要开发,目前还在beta版本有很多设计还不够完善,但是设计的拓展性没得说,flutter还是为我们提供了解决办法。我们可以强制widget不显示情况下保留状态,下回再加载时就不用重新创建了。
AutomaticKeepAliveClientMixin
AutomaticKeepAliveClientMixin 是一个抽象状态,使用也很简单,我们只需要用我们自己的状态继承这个抽象状态,并实现 wantKeepAlive 方法即可。
继承这个状态后,widget在不显示之后也不会被销毁仍然保存在内存中,所以慎重使用这个方法。
详细官方文档请看这里。
这里还有一个说的比较详细的 demo。
/*
* Created by 李卓原 on 2018/9/13.
* email: zhuoyuan93@gmail.com
*
*/
import ''package:flutter/material.dart'';
class TweetsListPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => TweetListState();
}
class TweetListState extends State<TweetsListPage>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
return Center(
child: Text(''TweetsListPage''),
);
}
@override
void dispose() {
print(''TweetsListPage dispose'');
super.dispose();
}
// 当页面不显示的时候也常驻在内存中
@override
bool get wantKeepAlive => true;
}
flutter TabBar 底部导航栏
更多文章请查看 lutter从入门 到精通
实现底部导航栏并点击切换页面可简述为有三种方式
- TabBar + TabBarView
- BottomNavigationBar + BottomNavigationBarItem
- 自定义 BottomAppBar
- 自定义绘制布局
在这里 使用 TabBar + TabBarView 来实现,与后两者明显不同的是,前者页面可以滑动.
TabBar 是一排水平的标签,可以来回切换.
import ''package:flutter/material.dart'';
/**
* 有状态StatefulWidget
* 继承于 StatefulWidget,通过 State 的 build 方法去构建控件
*/
class BotomeMenumTabBarPage extends StatefulWidget {
通过构造方法传值
BotomeMenumTabBarPage();
//主要是负责创建state
@override
BotomeMenumTabBarPageState createState() => BotomeMenumTabBarPageState();
}
/**
* 在 State 中,可以动态改变数据
* 在 setState 之后,改变的数据会触发 Widget 重新构建刷新
*/
class BotomeMenumTabBarPageState extends State<BotomeMenumTabBarPage> with SingleTickerProviderStateMixin{
BotomeMenumTabBarPageState();
TabController tabController;
@override
void initState() {
///初始化,这个函数在生命周期中只调用一次
super.initState();
tabController = new TabController(length: pages.length, vsync: this);
}
@override
Widget build(BuildContext context) {
//构建页面
return buildBottomTabScaffold();
}
//当前显示页面的
int currentIndex = 0;
//点击导航项是要显示的页面
final pages = [
ChildItemView("首页"),
ChildItemView("发现"),
ChildItemView("动态"),
ChildItemView("我的")
];
Widget buildBottomTabScaffold() {
return new Scaffold(
body: new TabBarView(controller: tabController, children:pages),
bottomNavigationBar: new Material(
color: Colors.blue,
child: new TabBar(
controller: tabController,
tabs: <Tab>[
new Tab(text: "首页", icon: new Icon(Icons.home)),
new Tab(text: "发现", icon: new Icon(Icons.find_in_page)),
new Tab(text: "动态", icon: new Icon(Icons.message)),
new Tab(text: "我的", icon: new Icon(Icons.person)),
],
indicatorWeight: 0.1,
),
),
);
}
/*切换页面*/
void _changePage(int index) {
/*如果点击的导航项不是当前项 切换 */
if (index != currentIndex) {
setState(() {
currentIndex = index;
});
}
}
}
//子页面
class ChildItemView extends StatefulWidget {
String _title;
ChildItemView(this._title);
@override
_ChildItemViewState createState() => _ChildItemViewState();
}
class _ChildItemViewState extends State<ChildItemView> {
@override
Widget build(BuildContext context) {
return Container(
child: Center(child: Text(widget._title)),
);
}
}
本文同步分享在 博客“早起的年轻人”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
Flutter TabBar 标签栏背景颜色、点击水波纹颜色配置
题记
—— 执剑天涯,从你的点滴积累开始,所及之处,必精益求精。
Flutter是谷歌推出的最新的移动开发框架。
【x1】微信公众号的每日提醒 随时随记 每日积累 随心而过 文章底部扫码关注
【x2】各种系列的视频教程 免费开源 关注 你不会迷路
【x3】系列文章 百万 Demo 随时 复制粘贴 使用
如下图所示,默认情况下使用 TabBar来实现的标签栏,是有点击水波纹效果以及使用的默认配置的背景颜色。
而在 App 开发的很多默认的使用场景中,多多少少是满足不了我们的需要的,如下图修改后的颜色效果。
1 测试 Demo 中 TabBar 应用在 AppBar 中
页面的主结构是通过 Scaffold 来搭建的,通过 TabBar 与 TabBarView 联动实现的效果。
class TestTabBarHomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SliderHomePageState();
}
}
class SliderHomePageState extends State with SingleTickerProviderStateMixin {
///Tab 与 TabView 联动使用的控制器
TabController tabController;
//当前显示页面的
int currentIndex = 0;
//点击导航项是要显示的页面
final pages = [Text("首页"), Text("发现"), Text("动态"), Text("我的")];
@override
void initState() {
///初始化,这个函数在生命周期中只调用一次
super.initState();
tabController = new TabController(length: pages.length, vsync: this);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("测试 Tab "),
bottom: buildPreferredSize(),
),
body: new TabBarView(controller: tabController, children: pages),
);
}
... ...
}
AppBar 的bottom 需要接收一个 PreferredSizeWidget 类型的组件,TabBar 就是继承于此,不过在这里 使用到了 PreferredSize ,这样的好处是我们可以在 AppBar 的bottom 使用其他任意的组件:
PreferredSize buildPreferredSize() {
return PreferredSize(
preferredSize: Size(0, 84),
child: buildTabBar(),
//child: buildTheme(),
);
}
Widget buildTabBar() {
return new TabBar(
controller: tabController,
tabs: <Tab>[
new Tab(text: "首页", icon: new Icon(Icons.home)),
new Tab(text: "发现", icon: new Icon(Icons.find_in_page)),
new Tab(text: "动态", icon: new Icon(Icons.message)),
new Tab(text: "我的", icon: new Icon(Icons.person)),
],
indicatorWeight: 0.1,
);
}
这样使用的 TabBar 是使用了默认配置的主题中的点击相关的颜色,如果需要修改,需要结合 Theme 来做。
2 通过 Theme 来个性 TabBar 的颜色
Theme buildTheme() {
return Theme(
data: ThemeData(
///默认显示的背景颜色
backgroundColor: Colors.blue[500],
///点击的背景高亮颜色
highlightColor: Colors.blueGrey[600],
///点击水波纹颜色
splashColor: Color.fromRGBO(0, 0, 0, 0),
),
child: new TabBar(
controller: tabController,
tabs: <Tab>[
new Tab(text: "首页", icon: new Icon(Icons.home)),
new Tab(text: "发现", icon: new Icon(Icons.find_in_page)),
new Tab(text: "动态", icon: new Icon(Icons.message)),
new Tab(text: "我的", icon: new Icon(Icons.person)),
],
indicatorWeight: 0.1,
),
);
}
以小编的性格,要实现百万Demo随时复制粘贴肯定是需要源码的
完整源码在这里
当然以小编的性格,肯定是要有视频录制的,目前正在录制中,你可以关注一下 西瓜视频 — 早起的年轻人 随后会上传
本文同步分享在 博客“早起的年轻人”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
How to create tabbar with ESTabBarController in swift 3.0
How to create tabbar with ESTabBarController in swift 3.0
First Install CocoaPods
- Open terminal and cd ~ to your project directory
- Run the command - pod init
- Your podfile should be use with - pod "ESTabBarController-swift" and save it
- And install it with command pod install
Open project file of .xcworkspace extension
- In project we need to add Content all swift class and pop.framework
- Don''t add pop.framework using add File to. you must be add from Framework and add Others.
- In Content folder''s all file import ESTabBarController_swift
StoryBord Stuff
Add navigation Controller ane also add ExampleNavigationController from the example code of EST demo. (You can add your own too) but make sure you set its Class of navigation custom swift class.
Code Stuff at AppDelegate.swift
You need to do following code AppDelegate.swift
//
// AppDelegate.swift
// DNApp
//
// Created by 雷神 on 2017/9/6.
// Copyright © 2017年 aider.cc. All rights reserved.
//
import UIKit
import CoreData
import ESTabBarController_swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UITabBarControllerDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let tabBarController = ESTabBarController()
tabBarController.delegate = self
tabBarController.title = "Irregularity"
tabBarController.tabBar.shadowImage = UIImage(named: "transparent")
tabBarController.tabBar.backgroundImage = UIImage(named: "background_dark")
tabBarController.shouldHijackHandler = {
tabbarController, viewController, index in
if index == 2 {
return true
}
return false
}
tabBarController.didHijackHandler = {
[weak tabBarController] tabbarController, viewController, index in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
let alertController = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
let takePhotoAction = UIAlertAction(title: "Take a photo", style: .default, handler: nil)
alertController.addAction(takePhotoAction)
let selectFromAlbumAction = UIAlertAction(title: "Select from album", style: .default, handler: nil)
alertController.addAction(selectFromAlbumAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
tabBarController?.present(alertController, animated: true, completion: nil)
}
}
let v1 = ExampleViewController()
let v2 = ExampleViewController()
let v3 = ExampleViewController()
let v4 = ExampleViewController()
let v5 = ExampleViewController()
v1.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
v2.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: "Find", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))
v3.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: "Photo", image: UIImage(named: "photo"), selectedImage: UIImage(named: "photo_1"))
v4.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: "Favor", image: UIImage(named: "favor"), selectedImage: UIImage(named: "favor_1"))
v5.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(), title: "Me", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))
tabBarController.viewControllers = [v1, v2, v3, v4, v5]
let navigationController = ExampleNavigationController.init(rootViewController: tabBarController)
tabBarController.title = "Example"
self.window?.rootViewController = navigationController
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application''s managed object context before the application terminates.
self.saveContext()
}
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "DNApp")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
今天关于swift TabBar和swift tabbar颜色选择器的分享就到这里,希望大家有所收获,若想了解更多关于Flutter TabBar、flutter TabBar 底部导航栏、Flutter TabBar 标签栏背景颜色、点击水波纹颜色配置、How to create tabbar with ESTabBarController in swift 3.0等相关知识,可以在本站进行查询。
本文标签: