繁体   English   中英

如何放置一个静态UIButton覆盖UITableView

[英]How to Put a static UIButton Overlaying a UITableView

基本上,无论表格视图是否滚动,我都想在顶部创建一个静态UIButton。 但是,我尝试通过使用“ bringSubviewToFront”方法将UIButton添加为UIView的子视图,并使它位于顶部。 但是,当我滚动UITableView时,UIButton仍会移动。 因此,如何制作覆盖UITableView的静态UIButton?

这是我的代码:

#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:滚动视图委托方法中重置视图的框架,但我认为视图仍会有些抖动。

最后,您可以将按钮直接添加到UIWindow ,但这可能会带来很多其他与旋转,动画和过渡有关的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM