特殊控制器
UINavigationController
#import "AppDelegate.h"
#import "ViewController.h"
@interfaceAppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
[self.windowmakeKeyAndVisible];
ViewController *vc = [[ViewControlleralloc]init];
UINavigationController *nvc = [[UINavigationControlleralloc]initWithRootViewController:vc];
self.window.rootViewController = nvc;
returnYES;
}
import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorbrownColor];
//延迟调用
//[self performSelector:@selector(hidden) withObject:nil afterDelay:1];
//导航条默认半透明,这时会遮挡self.view的64个像素的高度(tableView除外),为了避免这种情况,我们可以将导航条的半透明效果关掉
self.navigationController.navigationBar.translucent = NO;
self.title = @"导航条";//设置导航条标题
self.navigationController.navigationBar.titleTextAttributes = [NSDictionarydictionaryWithObjectsAndKeys:[UIColorwhiteColor],NSForegroundColorAttributeName ,nil];//设置导航条标题颜色
/***************************自定义导航条左按钮*************************/
UIButton *back = [UIButtonbuttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 30, 40);
[back setImage:[UIImageimageNamed:@"back"] forState:UIControlStateNormal];
[back addTarget:selfaction:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItemalloc]initWithCustomView:back];
self.navigationItem.leftBarButtonItem = item;
/***************************自定义导航条左按钮*************************/
/***************************自定义导航条左按钮*************************/
UIButton *rightbtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
rightbtn.frame = CGRectMake(0, 0, 40, 30);
[rightbtn setImage:[UIImageimageNamed:@"chatWith"] forState:UIControlStateNormal];
[rightbtn addTarget:selfaction:@selector(chat) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:rightbtn];
self.navigationItem.rightBarButtonItem = item1;
/***************************自定义导航条左按钮*************************/
}
-(void)back
{
[self.navigationControllerpopViewControllerAnimated:YES];//返回导航器的上一级
}
-(void)chat
{
ChatViewController *chatvc = [[ChatViewControlleralloc]init];
[self.navigationControllerpushViewController:chatvc animated:YES];//跳转到下一界面
}
-(void)hidden{
[self.navigationControllersetNavigationBarHidden:YESanimated:NO];//让导航栏延时之后消失
}
@end
TarBarViewController
ViewController *vc1 = [[ViewControlleralloc]init];
SecViewController *vc2 = [[SecViewControlleralloc]init];
vc2.tabBarItem.title = @"第二个";//设置tabbaritem的标题
vc2.tabBarItem.image = [UIImage imageNamed:@"addPhoto"];
UIImage *image = [[UIImageimageNamed:@"1"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//通过该方式,避免系统总显示默认的颜色
UIImage *seleimage = [[UIImageimageNamed:@"favorited"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"2" image:image selectedImage:seleimage];
item.badgeValue = @"5";
vc2.tabBarItem = item;
tabbar.tabBar.tintColor = [UIColor redColor];//选中颜色
tabbar.selectedIndex = 1;//默认选中第几个标签,(默认是0)
tabbar.tabBar.barTintColor = [UIColor brownColor];//背景色
tabbar.viewControllers = @[vc1,vc2];//设置tabbarcontroller对应的视图控制器
// tabbar.selectedViewController = vc1;//默认选中的视图控制器(必须存在于其viewcontrollers);
self.window.rootViewController = tabbar;
UINavigationController与TarBarViewController的结合
#import "AppDelegate.h"
#import "CookbookViewController.h"
#import "RecommendViewController.h"
#import "SearchViewController.h"
@interfaceAppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
[self.windowmakeKeyAndVisible];
UINavigationController *cookbooknvc = [[UINavigationControlleralloc]initWithRootViewController:[[CookbookViewControlleralloc]init]];
UINavigationController *recommendnvc = [[UINavigationControlleralloc]initWithRootViewController:[[RecommendViewControlleralloc]init]];
UINavigationController *seachnvc = [[UINavigationControlleralloc]initWithRootViewController:[[SearchViewControlleralloc]init]];
UITabBarController *tab = [[UITabBarControlleralloc]init];
tab.viewControllers = @[cookbooknvc,recommendnvc,seachnvc];
self.window.rootViewController = tab;
[[tab.tabBar.itemsobjectAtIndex:0] setTitle:@"家庭美食"];
[[tab.tabBar.itemsobjectAtIndex:1] setTitle:@"热门推荐"];
[[tab.tabBar.items objectAtIndex:2] setTitle:@"快速查找"];
//设置item选中和未选中的图标
[[tab.tabBar.itemsobjectAtIndex:0] setImage:[UIImageimageNamed:@"22"]];
[[tab.tabBar.itemsobjectAtIndex:1] setImage:[UIImageimageNamed:@"favorite"]];
[[tab.tabBar.itemsobjectAtIndex:2] setImage:[UIImageimageNamed:@"search_icon6"]];
[[UITabBarItemappearance] setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorcolorWithRed:178.0/255.0green:178.0/255.0blue:178.0/255.0alpha:1.0f], NSForegroundColorAttributeName, [UIFontsystemFontOfSize:11], NSFontAttributeName, nil] forState:UIControlStateNormal];
[[UITabBarItemappearance] setTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIColorcolorWithRed:64.0/255.0green:64.0/255.0blue:93.0/255.0alpha:1.0f], NSForegroundColorAttributeName, [UIFontsystemFontOfSize:11], NSFontAttributeName,nil] forState:UIControlStateSelected];
//改变UITabBarItem的title的位置
[[UITabBarItemappearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -2.0)];
returnYES;
}