困难是成功的阶梯,爬着很累,但不能轻言放弃。
把经验藏在肚子里的人,不会成功
首先在AppDelegate.m中
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建导航
ViewController *vv=[[ViewController alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vv];
self.window.rootViewController=nav;
return YES;
}
之后再ViewController.m中
#import "ViewController.h"
#define IPHONE_WIDTH [UIScreen mainScreen].bounds.size.width
#define IPHONE_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView * table;
UIView * _myNavigationView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title =@"向下滑动表格显示导航";
self.view.backgroundColor =[UIColor blackColor];
table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
table.backgroundColor =[UIColor whiteColor];
table.delegate =self;
table.dataSource =self;
[self.view addSubview:table];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
_myNavigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 64)];
_myNavigationView.backgroundColor = [UIColor redColor];
[self.view addSubview:_myNavigationView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString * cellID =@"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 1;
}
- ( UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
{
return nil;
}
- ( UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
return nil;
}
#pragma mark - scroll Delegate
//重要的东西都在这个方法里
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int contentOffSetY = scrollView.contentOffset.y;
if (contentOffSetY< 64)
{ _myNavigationView.alpha= scrollView.contentOffset.y/64; }
else
{
_myNavigationView.alpha=1;
}
if (contentOffSetY<0)
{
// 突然滑动的并且幅度很大的请款下 会在原本的导航view上面64的高度
// CGRect rect =_myNavigationView.frame; rect.origin.y =0;
// rect.size.height=-contentOffSetY+64;
// _myNavigationView.frame =rect;
}
}
@end
baige 1.gif
网友评论