问题描述
基本上,我想在顶部创建一个静态UIButton,无论表格视图是否滚动。但是,我尝试将UIButton添加为UIView的子视图,并使用bringSubviewToFront方法将其置于顶部。但是,当我滚动UITableView时,UIButton仍会移动。因此,如何使静态UIButton覆盖UITableView?
Basically, I wanna create a static UIButton on top whether or not the table view is scrolled. However, I tried to add the UIButton as the subview of the UIView and make it on the top by using "bringSubviewToFront" method. However, the UIButton still moves when I scroll the UITableView. Therefore, how can I make a static UIButton overlaying the 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是表视图。表视图是一个滚动视图,因此视图与其他所有内容一起滚动。
You're using a table view controller, so self.view is the table view. A table view is a scroll view, so the view scrolls along with everything else.
您应该使用带有表作为子视图的常规视图控制器。
You should use a regular view controller with a table as a subview instead.
或者,你可以尝试在 scrollViewDidScroll:
滚动视图委托方法中重置视图的框架,但我认为视图仍然是抖动了一下。
Alternatively, you could try resetting the view's frame in the scrollViewDidScroll:
scroll view delegate method, but I think the view would still jitter a bit.
最后,你可以直接将按钮添加到 UIWindow
,但这可能会带来旋转,动画和过渡等一系列其他问题。
Finally, you could add the button directly to the UIWindow
, but that's likely to bring up a whole host of other problems with rotations, animations, and transitions.
这篇关于如何放置静态UIButton覆盖UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!