我很难从UITableViewController的viewDidLoad方法中添加子视图(UIView
这是可行的:
[self.view addSubview:self.progView];但是您可以看到表格单元格线在UIView progView中溢出。
我尝试过这种方法:
[self.view.superview insertSubview:self.progView aboveSubview:self.view];这是将progView、UIView添加到当前视图上方的超级视图的尝试。当我尝试这样做时,UIView永远不会出现。
-更新--
以下是最新的尝试:
UIView *myProgView = (UIView *)self.progView; //progView is a method that returns a UIView
[self.tableView insertSubview:myProgView aboveSubview:self.tableView];
[self.tableView bringSubviewToFront:myProgView];结果与self.view addSubview:self.progView相同;UIView出现在表的后面。
发布于 2020-02-26 00:11:17
要在当前的UITableViewController之上添加一个customView,像丹尼尔评论的那样使用'self.navigationController.view addSubview:customView‘一定是一个很好的方式。
然而,在实现作为navigationBar的customView的情况下,丹尼尔的方法可能会导致在UITableViewController前面和后面的其他navigationViewControllers上默认或自定义navigationBar出现意外的结果。
最简单的方法就是将UITableViewController转换成UIViewController,它对子视图的布局没有限制。但是,如果您正在为大量、长时间遗留的UITableViewController代码而苦苦挣扎,情况就完全不同了。我们没时间转换了。
在这种情况下,您可以简单地劫持UITableViewController的tableView并解决整个问题。
我们应该知道的最重要的事情是UITableViewController的'self.view.superview‘是nil,而'self.view’是UITableView本身。
首先,劫持UITableVIew。
UITableView *tableView = self.tableView;然后,用新的UIView替换‘self.view’(现在是UITableView),这样我们就可以不受限制地布局customViews了。
UIView *newView = UIView.new;
newView.frame = tableView.frame;
self.view = newView;然后,把我们之前劫持的UITableView放在新的self.view上。
[newView addSubview:tableView];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[tableView.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
[tableView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[tableView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
[tableView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;现在,我们可以在UITableViewController上对这个全新的花哨的'self.view‘做任何我们想做的事情。
带来一个自定义视图,只需添加为subView即可。
UIView *myNaviBar = UIView.new;
[myNaviBar setBackgroundColor:UIColor.cyanColor];
[self.view addSubview:myNaviBar];
myNaviBar.translatesAutoresizingMaskIntoConstraints = NO;
[myNaviBar.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
[myNaviBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[myNaviBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
[myNaviBar.heightAnchor constraintEqualToConstant:90].active = YES;https://stackoverflow.com/questions/4641879
复制相似问题