UINavgation 总结

来源:互联网 发布:免费名师讲课软件 编辑:程序博客网 时间:2024/06/03 13:53

1.设置NavigationBar上左侧返回按钮

1.1设置button放在leftButtonItem上,但是动画效果和系统的不一样。(还未解决)

- (void)setLeftBarButtonItemWithImage:(NSString *)imageStr andText:(NSString *)text{    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];    [leftButton setFrame:CGRectMake(0, 0, 60, 30)];    [leftButton setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];    [leftButton setTitle:text forState:UIControlStateNormal];    if (text.length > 2) {        [leftButton setFrame:CGRectMake(0, 0, 90, 30)];        leftButton.titleLabel.font = [UIFont systemFontOfSize:17];    }    [leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];    //[leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];    [leftButton addTarget:self action:@selector(backWard) forControlEvents:UIControlEventTouchUpInside];     UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];    // iOS7以后自定义view的leftBarButtonItem偏右的调整  leftBarButtonItem 在 iOS7 下面的位置    if (IOS(7.0)) {        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];        negativeSpacer.width = -15;        self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftButtonItem];    }else{        self.navigationItem.leftBarButtonItem = leftButtonItem;    }}

1.2 设置只有文字的

- (void)setLeftBarButtonItemWithString:(NSString*)string{    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc]initWithTitle:string style:UIBarButtonItemStylePlain target:self action:@selector(backWard)];    leftButtonItem.tintColor                       = [UIColor whiteColor];    //    if (iOS(7)) {    //        [button setTitlePositionAdjustment:UIOffsetMake(5,0) forBarMetrics:UIBarMetricsDefault];    //    }    //1    self.navigationItem.leftBarButtonItem = leftButtonItem;    //2    //self.navigationItem.backBarButtonItem  = leftButtonItem;}

1.3 设置只有图片的

- (void)setLeftBarButtonItemWithImage:(NSString *)imageStr{    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:imageStr] style:UIBarButtonItemStyleDone target:self action:@selector(backWard)];    self.navigationItem.leftBarButtonItem = leftItem;}

2.NavigationBar 右侧设置图片

- (void)setRightBarButtonItemWithImage:(NSString *)imageStr{    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:imageStr] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemClick)];    self.navigationItem.rightBarButtonItem = rightItem;}

3.在UIScrollView上放可以左右滑动的多个UITabView

宽高:

CGFloat width = SCREENWIDTH;
CGFloat height = SCREENHEIGHT - StatusbarHeight - NavigationBarHeight;

...1myScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];...2 TableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain]; ...3 TableView2 = [[UITableView alloc]initWithFrame:CGRectMake(width, 0, width, height) style:UITableViewStylePlain]; [self.view addSubview:myScrollView]; [myScrollView addSubview:TableView1]; [myScrollView addSubview:TableView2];

4 设置backBarButtonItem

假若想实现BViewController的backBarButtonItem,就要在AViewController的ViewDidLoad中设置backBarButtonItem.

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:"返回"  style:UIBarButtonItemStylePlain  target:self  action:nil];self.navigationItem.backBarButtonItem = backButton;

这样才可以在B中设置backBarButtonItem.此时,才会在B中有效果出现,显示返回字样。否则,则无设置效果。

导航栏的讲解(iOS7以后)

参考链接:http://www.tuicool.com/articles/IZFRJbN

    iOS中导航栏默认变成了半透明的颜色。默认情况下,导航栏的translucent属性为YES。另外,系统还会对所有的导航栏做模糊处理,这样可以让iOS 7中导航栏的颜色更加饱和。    要想禁用translucent属性,可以在Storyboard中选中导航栏,然后在Attribute Inspectors中,取消translucent的勾选。    或者,self.navigationController.navigationBar.translucent = NO;

1.设置导航栏的背景颜色

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]  [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)]; 

2.在导航栏中使用背景图片

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@ "nav_bg.png" ] forBarMetrics:UIBarMetricsDefault]; 

如果希望在导航栏中使用一个图片当做背景,那么你需要提供一个稍微高一点的图片(这样可以延伸到导航栏背后)。导航栏的高度从44 points(88 pixels)变为了64 points(128 pixels)。

3.定制返回按钮的颜色

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; 除了返回按钮,tintColor属性会影响到所有按钮标题和图片。

如果想要用自己的图片替换V型,可以设置图片的backIndicatorImage和backIndicatorTransitionMaskImage。在didFinishLaunchingWithOptions:中设置

/*替换 navigationItem.backBarButtonItem 上的 “<” 型图片 ,变成自定义的*///indicator 指示器  transition 过渡;转变   metrics ['metrɪks]  度量    [[UINavigationBar appearance]setBackIndicatorImage:[UIImage imageNamed:@"backImage@2x"]];    [[UINavigationBar appearance]setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"backImage@2x"]];

4. 修改导航栏标题的字体

UIKIT_EXTERN NSString *const UITextAttributeFont NS_DEPRECATED_IOS(5_0, 7_0, "Use NSFontAttributeName");// Key to the text color in the text attributes dictionary. A UIColor instance is expected.UIKIT_EXTERN NSString *const UITextAttributeTextColor NS_DEPRECATED_IOS(5_0, 7_0, "Use NSForegroundColorAttributeName");// Key to the text shadow color in the text attributes dictionary.  A UIColor instance is expected.UIKIT_EXTERN NSString *const UITextAttributeTextShadowColor NS_DEPRECATED_IOS(5_0, 7_0, "Use NSShadowAttributeName with an NSShadow instance as the value");// Key to the offset used for the text shadow in the text attributes dictionary. An NSValue instance wrapping a UIOffset struct is expected.UIKIT_EXTERN NSString *const UITextAttributeTextShadowOffset NS_DEPRECATED_IOS(5_0, 7_0, "Use NSShadowAttributeName with an NSShadow instance as the value");

使用导航栏的titleTextAttributes属性来定制导航栏的文字风格。在text attributes字典中使用如下一些key,可以指定字体、文字颜色、文字阴影色以及文字阴影偏移量:
UITextAttributeFont – 字体key –NSFontAttributeName

UITextAttributeTextColor – 文字颜色key –NSForegroundColorAttributeName

UITextAttributeTextShadowColor – 文字阴影色key – NSShadowAttributeName

UITextAttributeTextShadowOffset – 文字阴影偏移量key – NSShadowAttributeName

如下代码所示,对导航栏的标题风格做了修改:NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]; shadow.shadowOffset = CGSizeMake(0, 1); [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:                                                        [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,                                                         shadow, NSShadowAttributeName,                                                          [UIFont fontWithName:@ "HelveticaNeue-CondensedBlack"  size:21.0], NSFontAttributeName, nil]]; 

5. 修改导航栏标题为图片
如果要想将导航栏标题修改为一个图片或者logo,那么只需要使用下面这行代码即可:

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@ "appcoda-logo.png" ]]; 

6. 添加多个按钮
我们可以在导航栏左边或者右边添加多个按钮。例如,我们希望在导航栏右边添加一个照相机和分享按钮,那只需要使用下面的代码即可:

UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil]; UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil]; NSArray *actionButtonItems = @[shareItem, cameraItem]; self.navigationItem.rightBarButtonItems = actionButtonItems; 

7. 修改状态栏的风格
在iOS 7以后,我们可以修改每个view controller中状态栏的外观。通过UIStatusBarStyle常量可以指定状态栏的内容是暗色或亮色。默认情况下,状态栏的显示是暗色。
我们可以在每个view controller中overridingpreferredStatusBarStyle:方法,如下所示:

-(UIStatusBarStyle)preferredStatusBarStyle {      return  UIStatusBarStyleLightContent; } 

通过上面的方法来修改状态栏风格非常的棒。另外,我们也可以使用UIApplication的statusBarStyle方法来设置状态栏,不过,首先需要停止使用View controller-based status bar appearance。在project target的Info plist中,插入一个新的key,名字为View controller-based status bar appearance,并将其值设置为NO。
info.plist
然后就可以使用下面的代码来设置状态栏风格了:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 

8. 隐藏状态栏
有时候我们需要隐藏状态栏,那么此时我们在view controller中override方法prefersStatusBarHidden:即可,如下代码所示:

- (BOOL)prefersStatusBarHidden {      return  YES; } 
0 0