类继承于UIViewController,底部添加按钮必须继承于UIViewController,如果继承于UITableViewController底部的按钮会跟着一起滚动,不会悬浮在底部。直接上代码,其实没几行代码,这里我把完整的代码贴出来
@interface MyTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView * tableView;
@property (nonatomic, strong)UIView * bottomButtonView;
@property (nonatomic,assign)CGFloat offsetY;
@end```
```codelang
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"高仿简书文章详情页面";
self.offsetY = 0.0;
[self initTableView];
[self creatButton];
[self initStateBarView];
}```
```codelang
-(void)initStateBarView{
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 20)];
view.backgroundColor = [UIColor whiteColor];//状态栏颜色
[self.view addSubview:view];
}```
```codelang
-(void)initTableView{
//初始化UITableView
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:_tableView];
}```
```codelang
-(void)creatButton{
//创建底部按钮
self.bottomButtonView = [[UIView alloc]init];
self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50);
self.bottomButtonView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.bottomButtonView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, self.view.frame.size.width / 2, 50);
[button setTitle:@"插播广告" forState:UIControlStateNormal];
button.backgroundColor =[UIColor orangeColor];
button.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
button.layer.borderWidth = 1;
[self.bottomButtonView addSubview:button];
UIButton * butn= [UIButton buttonWithType:UIButtonTypeCustom];
butn.frame = CGRectMake(button.frame.size.width , 0, self.view.frame.size.width / 2, 50);
[butn setTitle:@"推广联盟" forState:UIControlStateNormal];
butn.backgroundColor =[UIColor redColor];
butn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
butn.layer.borderWidth = 1;
[self.bottomButtonView addSubview:butn];
}```
```codelang
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellIndetifi = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifi];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndetifi];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
// Configure the cell...
return cell;
}```
```codelang
#pragma mark ====== UIScrollViewDelegete ===========
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
self.offsetY = scrollView.contentOffset.y;
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y >self.offsetY ) {//向上滑动
//按钮消失
[UIView transitionWithView:self.bottomButtonView duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width, 50);
[self.navigationController setNavigationBarHidden:NO animated:YES];
} completion:NULL];
}else if (scrollView.contentOffset.y < self.offsetY ){//向下滑动
//按钮出现
[UIView transitionWithView:self.bottomButtonView duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50);
[self.navigationController setNavigationBarHidden:YES animated:YES];
} completion:NULL];
}
NSLog(@"偏移量是多少==%f",self.offsetY);
}```
```codelang
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];
if (translation.y>0) {
//按钮消失
[UIView transitionWithView:self.bottomButtonView duration:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50);
[self.navigationController setNavigationBarHidden:NO animated:YES];
} completion:NULL];
}else if(translation.y<0){
//按钮出现
[UIView transitionWithView:self.bottomButtonView duration:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
self.bottomButtonView.frame = CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width, 50);
[self.navigationController setNavigationBarHidden:YES animated:YES];
} completion:NULL];
}
}```
代码就这么多,下面上效果图
![仿简书文章详情页面.gif](https://img.qingshiwang.com/i969236/5de860d7bd426f8f.gif?imageMogr2/auto-orient/strip)
本站以现代、古代情诗为主,情诗网创办于2013年,以原创爱情诗歌、经典情诗、现代情诗、古代情诗、英文情诗、情诗绝句为主并收集古诗、古诗词、诗歌大全、诗词名句的文学门户。方便您下次继续阅读;可以放在浏览器的收藏夹中(快捷键Ctrl+D);或者看到喜欢或者有趣的诗词可以通过分享按钮给你的好友分享;情诗网是目前最全情诗大全网站之一。并欢迎广大诗歌爱好者阅览投稿!喜欢本站的话请大家把本站告诉给你朋友哦!地址是 www.qingshiwang.com !