向 UINavigationBar 添加子视图时将 UITableView 向下推
Push UITableView down when adding subview to UINavigationBar
我正在尝试实现一个应该出现在 UINavigationBar
下方的通知栏。问题是当通知出现时,UITableView
中的单元格没有被按下,因此隐藏在通知后面,如下所示:
我的代码如下所示:
@IBOutlet var notificationView: UIView!
let navBar = self.navigationController?.navigationBar
let navBarHeight = navBar?.frame.height
let notificationFrame = notificationView.frame
let nSetX = notificationFrame.origin.x
let nSetY = CGFloat(navBarHeight!)
let nSetWidth = self.view.frame.width
let nSetHight = notificationFrame.height
notificationView.frame = CGRect(x: nSetX, y: nSetY, width: nSetWidth, height: nSetHight)
self.navigationController?.navigationBar.addSubview(notificationView)
您的问题有几种解决方案,但对您来说最简单的可能是在顶部添加内容偏移量,如下所示:
self.tableView.contentInset = UIEdgeInsetsMake(newBar.height, 0, 0, 0)
@h44f33z 建议的另一个解决方案是在新栏和 tableView 之间添加一个约束,因此它类似于此(以可视格式):
"V:|-0-[newBar(\(newBar.height))]-0-[tableView]-0-|"
我正在尝试实现一个应该出现在 UINavigationBar
下方的通知栏。问题是当通知出现时,UITableView
中的单元格没有被按下,因此隐藏在通知后面,如下所示:
我的代码如下所示:
@IBOutlet var notificationView: UIView!
let navBar = self.navigationController?.navigationBar
let navBarHeight = navBar?.frame.height
let notificationFrame = notificationView.frame
let nSetX = notificationFrame.origin.x
let nSetY = CGFloat(navBarHeight!)
let nSetWidth = self.view.frame.width
let nSetHight = notificationFrame.height
notificationView.frame = CGRect(x: nSetX, y: nSetY, width: nSetWidth, height: nSetHight)
self.navigationController?.navigationBar.addSubview(notificationView)
您的问题有几种解决方案,但对您来说最简单的可能是在顶部添加内容偏移量,如下所示:
self.tableView.contentInset = UIEdgeInsetsMake(newBar.height, 0, 0, 0)
@h44f33z 建议的另一个解决方案是在新栏和 tableView 之间添加一个约束,因此它类似于此(以可视格式):
"V:|-0-[newBar(\(newBar.height))]-0-[tableView]-0-|"