#import
#define toolbarH 44
@interface ViewController () <UITableViewDelegate> @property(nonatomic, weak) UITableView *tableView; @property(nonatomic, weak) UIView *toolbar; @end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; UITableView *tableView = [[UITableView alloc] init]; tableView.frame = self.view.bounds; tableView.contentInset = UIEdgeInsetsMake(toolbarH, 0, 0, 0); tableView.delegate = self; [self.view addSubview:tableView]; _tableView = tableView; CGFloat toolbarW = self.tableView.bounds.size.width; UIView *toolbar = [[UIView alloc] init]; toolbar.frame = CGRectMake(0, -toolbarH, toolbarW, toolbarH); toolbar.backgroundColor = [UIColor orangeColor]; [self.tableView addSubview:toolbar]; _toolbar = toolbar; }
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat newY = 0; if (toolbarH + scrollView.contentOffset.y > 0) { UIWindow *window = [UIApplication sharedApplication].windows.lastObject; [window addSubview:self.toolbar]; newY = [self.tableView convertRect:self.tableView.bounds toView:window].origin.y; } else { [self.tableView addSubview:self.toolbar]; newY = -toolbarH; } CGRect frame = self.toolbar.frame; frame.origin.y = newY; self.toolbar.frame = frame; } @end
|
近期评论