首先是无故事板模式下
一 创建一个导航控制器
1 创建一个viewcontrol Test_OneViewController
@interface AppDelegate :UIResponder <UIApplicationDelegate>
{
Test_OneViewController *test;
UINavigationController *navigation;
}
@property (strong,nonatomic)UIWindow *window;
@property (strong,nonatomic)UINavigationController *navigation;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
self.window.backgroundColor = [UIColorwhiteColor];
test = [[Test_OneViewController alloc]init];
navigation = [[UINavigationController alloc]initWithRootViewController:test];
[self.window addSubview:navigation.view];
[self.windowmakeKeyAndVisible];
returnYES;
}
///
// Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.
//推送到下一个视图
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
// Returns the popped controller.
//返回上一个视图
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
// Pops view controllers until the one specified is on top. Returns the popped controllers.
返回一个指定的视图
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
// Pops until there's only a single view controller left on the stack. Returns the popped controllers.
//返回根视图
///
这时候我们们创建了一个导航控制器!在首页上我们会看见一个导航栏
二 在导航控制器上我们添加两个按钮 UIBarButtonItem
/*
typedefNS_ENUM(NSInteger, UIBarButtonItemStyle) {
UIBarButtonItemStylePlain, // shows glow when pressed
UIBarButtonItemStyleBordered,
UIBarButtonItemStyleDone,
};
- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (id)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(id)target action:
(SEL)actionNS_AVAILABLE_IOS(5_0);// landscapeImagePhone will be used for the bar button image in landscape bars in UIUserInterfaceIdiomPhone only
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
- (id)initWithCustomView:(UIView *)customView;
./// Test_OneViewController //
通过 - (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action 函数创建了两个UIBarButtonItem
self.title =@"导航控制器"; //导航标题
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"push"style:UIBarButtonItemStyleDone target:self action:@selector(pushAction)];
self.navigationItem.rightBarButtonItem = rightButton;
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"mode"style:UIBarButtonItemStyleDone target:self action:@selector(modeAction)];
self.navigationItem.leftBarButtonItem = leftButton;
// mode 方式推送到一个视图 这种情况通常用来选取数据,调用系统通讯录 设置 等 需要手动写取消返回
//typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
// UIModalTransitionStyleCoverVertical = 0,
// UIModalTransitionStyleFlipHorizontal,
//UIModalTransitionStyleCrossDissolve,
-(void)modeAction
{
ModeViewController *mode = [[ModeViewController alloc]init];
mode.modalTransitionStyle =UIModalTransitionStyleFlipHorizontal; //设置翻转动画效果
//mode.modalPresentationStyle = UIModalPresentationFormSheet; //显示外观动画
[self presentViewController:mode animated:YES completion:nil];
}
//反回到上一个视图
-(void)dissMissModeAction
{
[self dismissViewControllerAnimated:YEScompletion:nil];
}
// push 常用的推送方式,推送成功后下个视图会出现back 返回按钮
-(void)pushAction
{
PushViewController *push = [[PushViewControlleralloc]init];
[self.navigationControllerpushViewController:pushanimated:YES ];
}
问题 一 但是现在发现 返回按钮是英文的 应该改成汉语
我们在 添加左右item 的地方添加个backButtonItem
在ios7中有可以取消字母提示 用系统的图标更简洁 只要把title 设置为空就ok了
UIBarButtonItem *backButtonItem = [[UIBarButtonItemalloc]init];
//backButtonItem.title = @"返回";
backButtonItem.title = @"";
self.navigationItem.backBarButtonItem = backButtonItem;
问题二 发现导航栏是透明的 并且不暂用像素
我们可以通过设置bool 值 来隐藏导航栏
我们可以设置 bool 值来改变 透明不透明
self.navigationController.navigationBar.translucent =NO;
问题 三 有时候我们在自定义导航栏时候不想显示系统的导航栏怎么办
self.navigationController.navigationBarHidden =YES;
问题四 如何改变系统导航栏的颜色 和自定义标题
ios7 xcode5 环境下
UIColor *navigationBackColor = [UIColorcolorWithRed:82.0/255.0green:135.0/255.0blue:237.0/255.0alpha:1.0];
// ios7 新增加的方法 在xcode 5 下
self.navigationController.navigationBar.barTintColor = navigationBackColor;
// 这个方法可与改变字体的颜色
self.navigationController.navigationBar.tintColor = navigationBackColor;
能改变字体的颜色
ios 6.1 xcode 4 环境下
UIColor *navigationBackColor = [UIColor colorWithRed:82.0/255.0 green:135.0/255.0 blue:237.0/255.0 alpha:1.0];
self.navigationController.navigationBar.tintColor =navigationBackColor;
如何在ios6.1的 情况下需要自定义 title的
UILabel *navigateTitleLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,0,100,30)];
navigateTitleLabel.font = [UIFontsystemFontOfSize:20.0];
navigateTitleLabel.textColor = [UIColorblackColor];
navigateTitleLabel.backgroundColor = [UIColorclearColor];
navigateTitleLabel.textAlignment =NSTextAlignmentCenter;
navigateTitleLabel.text = @"导航控制器";
self.navigationItem.titleView = navigateTitleLabel;
// 用系统的item/
我么通过这个函数可以设置系统提供的Item
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
typedefNS_ENUM(NSInteger, UIBarButtonSystemItem) {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
我们可以先把导航左边的的Item 改成 UIBarButtonSystemItemSearch 一个搜索的图形
UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearchtarget:selfaction:@selector(modeAction)];
self.navigationItem.leftBarButtonItem = leftButton;
问题五 通过图片 改变back 按钮
// ITEM 添加图片/
- (id)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(id)target action:
我在back 所在的视图类添加一下代码 就把按钮替换成自己定义的风格图形的按钮了 同理当然可以改变push 的item
UIImage *imge = [UIImageimageNamed:@"导航_返回.png"];
UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithImage:imgestyle:UIBarButtonItemStyleDonetarget:selfaction:@selector(pushActions)];
self.navigationItem.leftBarButtonItem = leftButton;
// 然后通过 [self.navigationController popViewControllerAnimated:YES]; 返回上一级视图
-(void)pushActions
{
[self.navigationControllerpopViewControllerAnimated:YES];
}
问题 六 自定义一个导航栏按钮
- (id)initWithCustomView:(UIView *)customView;
我在返回按钮的视图里右边又加了一个返回按钮 但是通过自定义的方式
UIButton * rightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightbtn.frame =CGRectMake(8,10,25,20);
[rightbtn setBackgroundImage:imge forState:UIControlStateNormal];
UIBarButtonItem * revealButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightbtn];
self.navigationItem.rightBarButtonItem = revealButtonItem;
我们发现api 提供的这个方法的参数是个view如果加个button 不是很明显那么我们就添加个textfield 把 这样更直观
UITextField *textfield = [[UITextFieldalloc]initWithFrame:CGRectMake(8,10,80,25)];
textfield.placeholder =@"hello";
textfield.backgroundColor = [UIColorwhiteColor];
UIBarButtonItem * revealButtonItem = [[UIBarButtonItemalloc]initWithCustomView:textfield];
self.navigationItem.rightBarButtonItem = revealButtonItem;
问题 七 在导航栏中间添加分段控件
主要是通过这个函数 self.navigationItem.titleView = segment;
NSArray *buttonName = [NSArrayarrayWithObjects:@"周杰伦",@"张学友",@"王大那",nil];
UISegmentedControl *segment = [[UISegmentedControlalloc]initWithItems:buttonName];
segment.momentary =YES;
[segment addTarget:selfaction:@selector(segmengAction:)forControlEvents:UIControlEventEditingChanged];
self.navigationItem.titleView = segment;
问题八 导航右侧添加两个buttonItem
UIBarButtonItem *oneButton = [[UIBarButtonItemalloc]initWithTitle:@"first"style:UIBarButtonItemStyleDone target:self action:@selector(oneButtonAction:)];
UIBarButtonItem *twoButton = [[UIBarButtonItemalloc]initWithTitle:@"second"style:UIBarButtonItemStyleDone target:self action:@selector(twoButtonAction:)];
NSArray *actionButtonItems = @[oneButton, twoButton];
self.navigationItem.rightBarButtonItems = actionButtonItems;
问题九 添加导航栏背景图片
[navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"导航背景.png"] forBarMetrics:UIBarMetricsDefault];
问题十 知道系统返回按钮事件
在返回按钮的上一级页面
在 push 时指定delegate
[self.navigationControllerpushViewController:interestJobanimated:YES];
self.navigationController.delegate =self;
hideNavigationBar =NO;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (hideNavigationBar) {
// 返回事件
}
hideNavigationBar = YES;
}
问题十一 如何pop 回指定的页面
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] -2)] animated:YES];