狀態(tài)欄的設置
情形:設置歡迎頁影藏,首頁顯示:
在info.plist里增加 Status bar is initially hidden,設置為yes
然后在首頁或者父類控制器里設置
// MARK: - 狀態(tài)欄
override var prefersStatusBarHidden: Bool{
return false
}
這里需要注意第二個方法:
1、如果ViewController不是UINavigationController的子類,調(diào)用 preferredStatusBarStyle 是可以改變狀態(tài)欄文字的顏色,相反則不能;
因為 UINavigationController 有自己的狀態(tài)欄,需要自己管理所以它的子類是不會走 preferredStatusBarStyle 方法;如果想要某個VC 改變,可以使用 UINavigationBar.barStyle屬性
self.navigationController?.navigationBar.barStyle = .black
或者 //隱藏導航欄后 系統(tǒng)會調(diào)用 preferredStatusBarStyle 方法
self.navigationController.navigationBarHidden=YES;
標題
//tab和nav的title會全部更改:
self.title = @"首頁";
//nav會更改 tab不會更改:
self.navigationItem.title = @"首頁";
導航欄顏色
//修改導航欄的顏色
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
導航欄下個界面返回按鈕
//修改導航欄下個界面返回按鈕的文字 注意 修改的是下一個界面的返回按鈕:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
導航欄返回按鈕圖片
// 修改導航欄返回按鈕的圖片 返回按鈕圖片大小為42*42 這里不能直接按照下面的方式設置,不然當界面上有彈框UIAlertController時,圖片會向下偏移:
let leftBBi = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(personalSettingBack))
leftBBi.image = UIImage(named: "nav_back")
leftBBi.tintColor = HHGK_WHITE_COLOR
self.navigationItem.leftBarButtonItem = leftBBi
// 正確的做法應該是自定義一個按鈕, 然后設置為leftBarButtonItem:
let backBtn = UIButton(type: .custom)
backBtn.setImage(UIImage.init(named: "nav_back"), for: .normal)
backBtn.tintColor = HHGK_WHITE_COLOR
backBtn.frame = CGRect(x: 0, y: 0, width: 42, height: 42)
backBtn.addTarget(self, action: #selector(personalSettingBack), for: .touchUpInside)
let backBBi = UIBarButtonItem.init(customView: backBtn)
self.navigationItem.leftBarButtonItem = backBBi
導航欄文字樣式
//修改導航欄中間文字的樣式:
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
swift:
self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
工具欄的顏色:
//修改工具欄的顏色:
self.tabBarController.tabBar.barTintColor = [UIColor grayColor];
//修改工具欄下面文字的顏色:
self.tabBarController.tabBar.tintColor = [UIColor blackColor];
透明
//把頂部這個navigationbar設置為透明呢,能夠讓下面的圖片顯示出來,但是返回按鈕不透明:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
線條
//去掉導航欄下面的線條:
self.navigationController.navigationBar.shadowImage = [UIImage new];
未選中與選中時的圖片
//設置UITabBarItem未選中與選中時的圖片:
[_hotTabItem setFinishedSelectedImage:[UIImage imageNamed:@"1_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"1"]];
badgeValue
//設置UITabBarItem的badgeValue:
//指定界面
[self.tabBarController.tabBar.items objectAtIndex:2].badgeValue = [NSString stringWithFormat:@"%ld", ++ goodsCount];
//當前界面
_mine.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", count];
//為0時清除
_mine.navigationController.tabBarItem.badgeValue = nil;
tabBar樣式
//設置tabBar樣式
[[UITabBarItemappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[selfmyThemeColor]}forState:UIControlStateSelected];
[[UITabBarItemappearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorredColor]}forState:UIControlStateNormal];
//選中的顏色(圖片+圖片下面文字的顏色,iOS8.0以后被廢棄了)
self.tabBarController.tabBar.selectedImageTintColor= [selfmyThemeColor];
//未選中的顏色(圖片+圖片下面文字的顏色,沒被廢棄)
self.tabBarController.tabBar.unselectedItemTintColor= [UIColorgreenColor];
dismiss回到目標控制器
// dismiss方式回到目標控制器:
/*
(1) presentedViewController:The view controller that is presented by this view controlller(read-only),被本視圖控制器present出來的的視圖控制器(只讀)
(2) presentingViewController:The view controller that presented this view controller. (read-only),present出來本視圖控制器的視圖控制器(只讀)
*/
UIViewController *vc = self.presentingViewController;
while (![vc isKindOfClass:[TFTravelllerLoginViewController class]]) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:nil];
push到下個界面隱藏tabBar pop回去顯示tabBar
// 注意是設置目標控制器的hidesBottomBarWhenPushed屬性
EaseMessageViewController *messageVc = [[EaseMessageViewController alloc] initWithConversationChatter:contactName conversationType:EMConversationTypeChat];
messageVc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:messageVc animated:YES];
pop回到目標控制器
//pop方式回到目標控制器:
for (UIViewController *disVc in self.navigationController.viewControllers) {
if ([disVc isKindOfClass:[TFTravelllerLoginViewController class]]) {
[self.navigationController popToViewController:disVc animated:YES];
}
}
UISearchBar自定義
searchBar.backgroundImage = [CzyTools imageWithColor:[UIColor colorWithHexString:@"#F5F5F5"]];
searchBar.backgroundColor = [UIColor clearColor];
[searchBar setSearchFieldBackgroundImage:[CzyTools imageWithColor:[UIColor colorWithHexString:@"#F5F5F5"]] forState:UIControlStateNormal];