iOS 开发中导航栏渐变的两种方法
来源:互联网 发布:js 数组遍历 foreach 编辑:程序博客网 时间:2024/05/22 07:46
很多 APP 都有导航栏颜色渐变的效果,各位开发者也都八仙过海各显神通,各有各的方法,我这里提供两种思路:第一种是通过设置 navigationBar 的 alpha;第二种是在 navigationBar 上加入一个 view,再设置 view 的 alpha。
两种方法的核心代码:
一、设置 navigationBar 的 alpha
向上滑和向下滑时分别对 navigationBar 的透明度进行设置。
#pragma mark - UIScrollViewDelegate- (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.currentPostion = scrollView.contentOffset.y; if (self.currentPostion > 0) { if (self.currentPostion - self.lastPosition >= 0) { if (self.topBool) { self.topBool = NO; self.bottomBool = YES; self.stopPosition = self.currentPostion + 64; } self.lastPosition = self.currentPostion; self.navigationController.navigationBar.alpha = 1 - self.currentPostion / 500; } else { if (self.bottomBool) { self.bottomBool = NO; self.topBool = YES; self.stopPosition = self.currentPostion + 64; } self.lastPosition = self.currentPostion; self.navigationController.navigationBar.alpha = (self.stopPosition - self.currentPostion) / 200; } }}
这里要说明一点,如果你不希望自己 navigationBar 上的按钮也被改变透明度,可以通过自定义 UINavigationController 实现。用系统的 navigationBar 时,navigationBar上的按钮的透明度也会被更改。
二、在 navigationBar 上加入一个 view
在 navigationBar 上加一个 view,然后设置 view 的 alpha。
CGSize customSize = self.navigationController.navigationBar.frame.size; self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, customSize.width, customSize.height + 20)]; self.customView.backgroundColor = [UIColor orangeColor]; self.customView.userInteractionEnabled = NO; [self.navigationController.navigationBar insertSubview: self.customView atIndex:0];
这里也要说明一点,View 的 userInteractionEnabled 属性要设置为 NO,不然会出现问题。
UIScrollViewDelegate 的这个代理方法与上面的一样。
#pragma mark - UIScrollViewDelegate- (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.currentPostion = scrollView.contentOffset.y; if (self.currentPostion > 0) { if (self.currentPostion - self.lastPosition >= 0) { if (self.topBool) { self.topBool = NO; self.bottomBool = YES; self.stopPosition = self.currentPostion + 64; } self.lastPosition = self.currentPostion; self.customView.alpha = 1 - self.currentPostion / 500; } else { if (self.bottomBool) { self.bottomBool = NO; self.topBool = YES; self.stopPosition = self.currentPostion + 64; } self.lastPosition = self.currentPostion; self.customView.alpha = (self.stopPosition - self.currentPostion) / 200; } }}
上面的两种方法都可以实现导航栏渐变,只是思路不同,一个是改变系统的导航栏,一个是在导航栏上插入 View,另外还可以自定义导航控制器这个类来实现,各位道友可以试试哦!
代码传送门 欢迎各位道友交流或指出错误!
阅读全文
0 0
- iOS 开发中导航栏渐变的两种方法
- ios开发:导航栏navigationbar背景渐变
- iOS开发-玩玩渐变导航栏
- iOS开发Navigation 导航栏渐变
- iOS开发 - 渐变导航栏终极版
- iOS 导航栏设置颜色的两种方法
- iOS 隐藏导航栏底部线的两种方法
- iOS 为UIView添加渐变色的两种方法
- iOS 导航栏渐变
- IOS开发中发送Email的两种方法
- IOS开发中发送电子邮件的两种方法
- iOS 设置导航透明的两种方法
- iOS 设置导航透明的两种方法
- iOS 设置导航透明的两种方法
- iOS开发实现导航栏透明度渐变效果
- iOS 导航栏颜色渐变
- iOS中隐藏导航栏的方法
- iOS 开发中渐变色的使用
- English story 17
- bootstrap datatimepicker 兼容问题
- leetcode--Construct Binary Tree from Inorder and Postorder Traversal
- 九的余数
- 持久化类集合属性如何映射到数据库?
- iOS 开发中导航栏渐变的两种方法
- Kotlin&Anko基础教程大全 二
- python list去重
- AndroidStudio+Genymotion(附破解方法)安卓开发环境搭建
- js中的循环结构
- leetcode--Convert Sorted List to Binary Search Tree
- 剑指offer-序列化二叉树
- java分割字符串split()使用需注意的地方,转义字符使用
- windows下安装django(python2.7)并配置mysql