UINavigationController使用总结
1.自定义UINavigationController页面切换的动画
想自定义UINavigationController页面切换的动画,达到presentViewController和dismissViewController的效果,但是又能保存在UINavigationController的viewControllers里以方便各种POP操作,可以使用如下方式:
PUSH:
CATransition *animation = [CATransition animation]; [animation setDuration:0.3]; [animation setType: kCATransitionMoveIn]; [animation setSubtype: kCATransitionFromTop]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]]; UIStoryboard *storyboard = self.storyboard; UnknownDeviceListViewController *unknownViewController = [storyboard instantiateViewControllerWithIdentifier:@"bluetoothlist"]; [self.navigationController pushViewController:unknownViewController animated:NO]; [self.navigationController.view.layer addAnimation:animation forKey:nil];
POP:
CATransition *animation = [CATransition animation]; [animation setDuration:0.3]; [animation setType: kCATransitionMoveIn]; [animation setSubtype: kCATransitionFromBottom]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]]; [self.navigationController.view.layer addAnimation:animation forKey:nil]; [self.navigationController popViewControllerAnimated:NO];
注意,POP的时候,必须先addAnimation,然后在popViewController。否则如果animated为NO,则没有动画效果;如果animated为YES,则自带的POP动画和我们自定义的POP动画会一起生效。
2.几种POP方法:
[self.navigationController popViewControllerAnimated:YES];//上一层 [self.navigationController popToRootViewControllerAnimated:YES];//最顶层 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];//指定某一层
3.常用属性:
@property(nonatomic,readonly,retain) UIViewController *topViewController; // The top view controller on the stack. @property(nonatomic,readonly,retain) UIViewController *visibleViewController; // Return modal view controller if it exists. Otherwise the top view controller. @property(nonatomic,copy) NSArray *viewControllers; // The current view controller stack.
4.自定义返回按钮
#import "LeftArrowButton.h" @implementation LeftArrowButton - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"leftarrow.png"]]; imageView.frame = CGRectMake(-8, 5, 12.5, 20.5); imageView.tintColor = [UIColor whiteColor]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(11, 0, 40, 30)]; label.text = @"返回"; label.textColor = [UIColor whiteColor]; [self addSubview:imageView]; [self addSubview:label]; } return self; } - (void)dealloc{ [super dealloc]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ self.alpha = 0.2; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ self.alpha = 1; UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; if (point.x > self.frame.size.width || point.y >self.frame.size.height) return; [self.delegate didReturn]; } @end
白色箭头: 黑色箭头:
X.链接
- iOS 7中默认的导航栏
- 设置导航栏的背景颜色
- 在导航栏中使用背景图片
- 定制返回按钮的颜色
- 修改导航栏标题的字体
- 修改导航栏标题为图片
- 添加多个按钮
- 修改状态栏的风格
- 隐藏状态栏
- 总结