如何把一个静态的UIButton覆盖一个UITableView

基本上,我想创build一个静态的UIButton,无论是否滚动表视图。 但是,我试图添加UIButton作为UIView的子视图,并使用“bringSubviewToFront”方法在顶部。 但是,当我滚动UITableView UIButton仍然移动。 因此,我怎样才能使一个静态的UIButton覆盖UITableView?

这是我的代码:

#import "DiscoverTimelineTableViewController.h" @interface DiscoverTimelineTableViewController () @property (strong, nonatomic) UIView* myview; @end @implementation DiscoverTimelineTableViewController - (void)viewDidLoad { [super viewDidLoad]; [self displayMakePlanButton]; NSLog(@"%f", self.view.layer.zPosition); NSLog(@"%f", self.tableView.layer.zPosition); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 100.0f; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; // Configure the cell... cell.textLabel.text = @"Sleepy"; return cell; } #pragma mark - Add the make plan button - (void) displayMakePlanButton { CGFloat buttonWidth = self.view.frame.size.width; CGFloat buttonHeight = 104.0f; CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height; CGFloat statBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height; UIButton *makePlanButton = [UIButton buttonWithType: UIButtonTypeCustom]; [makePlanButton setFrame:CGRectMake(0, self.view.frame.size.height - buttonHeight - navBarHeight - statBarHeight, buttonWidth, buttonHeight)]; [makePlanButton setBackgroundColor:[UIColor colorWithRed:0.286 green:0.678 blue:0.231 alpha:1]]; [makePlanButton setTitle:@"MAKE PLAN" forState:UIControlStateNormal]; [self.view addSubview:makePlanButton]; //makePlanButton.layer.zPosition = 1; [self.view bringSubviewToFront:makePlanButton]; NSLog(@"%f", makePlanButton.layer.zPosition); } @end 

您正在使用表视图控制器,所以self.view是表视图。 表视图是一个滚动视图,所以视图和其他所有内容一起滚动。

您应该使用一个常规的视图控制器与一个表作为子视图。

或者,您可以尝试重置scrollViewDidScroll: scroll view委托方法中的视图的框架,但我认为该视图仍然会抖动一点。

最后,你可以直接将button添加到UIWindow ,但这可能会带来一系列其他旋转,animation和转换问题。