Add the hover button on TableView and the hover button on tableview.
If a Button is directly attached to the TableVIewController, this will scroll. The following two methods can be used to implement a fixed position floating Button on TableView:
1. paste tableView on the view, and then paste the floating button on the top of the view.
2. Use window
First, let's take a look at the final effect. Add a floating button on tableViewController, which cannot scroll with the view
First, we will introduce the first method above:
1) Create properties of tableview and button at the bottom
// Screen width
# Define kScreenW [UIScreen mainScreen]. bounds. size. width
// Screen height
# Define kScreenH [UIScreen mainScreen]. bounds. size. height
@ Interface broadcastLiveViewController () <UITableViewDataSource, UITableViewDelegate>
@ Property (nonatomic) UITableView * livesListTable;
@ Property (nonatomic) UIButton * bottomButton;
@ End
2) create an attribute to the top
@ Implementation broadcastLiveViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
CGRect clientRect = [UIScreen mainScreen]. bounds;
_ LivesListTable = [[UITableView alloc] initWithFrame: CGRectMake (0, 0, clientRect. size. width, clientRect. size. height-65) style: UITableViewStylePlain];
[Self. view addSubview: _ livesListTable];
_ LivesListTable. delegate = self;
_ LivesListTable. dataSource = self;
Self. bottomButton = [UIButton buttonWithType: UIButtonTypeCustom];
Self. bottomButton. frame = CGRectMake (kScreenW-80, kScreenH-140, 60, 60 );
[Self. bottomButton setBackgroundImage: [UIImage imageNamed: @ "recordLive"] forState: UIControlStateNormal];
[Self. bottomButton addTarget: self action: @ selector (onTapLiveBtn) forControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: self. bottomButton];
3) Implement button events
-(Void) onTapLiveBtn
{
NSLog (@ "click the bottom button ");
}
Next we will introduce the second method:
1) create a window. The button attribute prevents window from being released.
// Screen width
# Define kScreenW [UIScreen mainScreen]. bounds. size. width
// Screen height
# Define kScreenH [UIScreen mainScreen]. bounds. size. height
@ Interface broadcastLiveViewController () <UITableViewDataSource, UITableViewDelegate>
@ Property (strong, nonatomic) UIWindow * window;
@ Property (strong, nonatomic) UIButton * button;
@ End
2) create a window and a button
By default, the system has only one window. In this case, we need to set the windowLevel.
Windows do not need to be added to any view
-(Void) createButton {
_ Button = [UIButton buttonWithType: UIButtonTypeCustom]; [_ button setBackgroundImage: [UIImage imageNamed: @ "recordLive"] forState: UIControlStateNormal]; _ button. frame = CGRectMake (0, 0, 60, 60); [_ button addTarget: self action: @ selector (onTapLiveBtn) forControlEvents: UIControlEventTouchUpInside]; _ window = [[UIWindow alloc] initWithFrame: CGRectMake (kScreenW-80, kScreenH-80, 60, 60);]; _ window. windowLevel = UIWindowLevelAlert + 1; _ window. backgroundColor = [UIColor redColor]; _ window. layer. cornerRadius = 30; _ window. layer. masksToBounds = YES; [_ window addSubview: _ button]; [_ window makeKeyAndVisible]; // key statement, display window}
3) Delayed loading of the window. Note that we need to create this floating button after creating the rootWindow.
-(Void) viewDidLoad {[super viewDidLoad]; [self defined mselector: @ selector (createButton) withObject: nil afterDelay: 1];}
4) Implement button events
-(Void) onTapLiveBtn
{
NSLog (@ "click the bottom button ");
}
Note: add another small function to make the animation effect of the button appear and disappear when the tableview slides up and down. In this case, it is pulled up and down.
-(Void) scrollViewDidScroll :( UIScrollView *) scrollView {
If (scrollView. contentOffset. y> self. offsetY & scrollView. contentOffset. y> 0) {// move up
// Button disappears
[UIView transitionWithView: self. bottomButton duration: 0.1 options: UIViewAnimationOptionTransitionNone animations: ^ {
Self. bottomButton. frame = CGRectMake (kScreenW-80, kScreenH-65, 60, 60 );
} Completion: NULL];
} Else if (scrollView. contentOffset. y <self. offsetY) {// slide down
// Button appears
[UIView transitionWithView: self. bottomButton duration: 0.1 options: UIViewAnimationOptionTransitionNone animations: ^ {
Self. bottomButton. frame = CGRectMake (kScreenW-80, kScreenH-140, 60, 60 );
} Completion: NULL];
}
Self. offsetY = scrollView. contentOffset. y; // Changes the current displacement to the cache displacement.
}