Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:热点指示器抛出的视图定位_Ios_Swift_Uinavigationbar_Cgrect - Fatal编程技术网

iOS:热点指示器抛出的视图定位

iOS:热点指示器抛出的视图定位,ios,swift,uinavigationbar,cgrect,Ios,Swift,Uinavigationbar,Cgrect,遇到错误时,我们在表视图上显示一个带标签的矩形: 使用此代码: // Fix location of message bar even as user scrolls table - per http://stackoverflow.com/q/7537858/47281 override func scrollViewDidScroll(_ scrollView: UIScrollView) { let newY = self.tableView.contentOffset.

遇到错误时,我们在表视图上显示一个带标签的矩形:

使用此代码:

    // Fix location of message bar even as user scrolls table - per http://stackoverflow.com/q/7537858/47281
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let newY = self.tableView.contentOffset.y + self.navigationController!.navigationBar.frame.size.height + UIApplication.shared.statusBarFrame.height 
    // Show rectangle using 0 and newY ... 
它工作正常,但在某些情况下不起作用,例如启用个人热点时。导航栏和状态矩形之间存在间隙:

在导航栏下放置物品的正确方法是什么

在第一种情况下,状态栏高度为20,但在第二种情况下,状态栏高度为40。在这两种情况下,导航栏和内容偏移量相同。即:

  • 案例1:偏移量:-64.0,导航栏高度:44.0状态栏高度:20.0
  • 案例2:偏移量:-64.0,导航栏高度:44.0状态栏高度:40.0
看起来偏移量没有根据状态栏中的高度变化而增加


更新:参见(我的)已接受的答案。我相信有更好的办法。。如果您知道某个线程,请将其添加到该线程。

使用“自动布局”来定位消息,而不是使用从帧派生的值来定位消息

有几种方法可以实现这一点-一种方法是:

//... assuming this is being called from the parent view controller

    //safely unwrap the reference to the navigation controller
    guard let navController = self.navigationController else { return }
    let message = UIView()

    //arbitrary styles for example only
    message.backgroundColor = UIColor.blue
    //set translatesAutoresizingMaskIntoConstraints so constraints will work
    message.translatesAutoresizingMaskIntoConstraints = false

    //create constraints
    let height = NSLayoutConstraint(item: message, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 50)
    let equalWidth = NSLayoutConstraint(item: message, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: navController.navigationBar, attribute: .width, multiplier: 1.0, constant: 0)
    let alignBottom = NSLayoutConstraint(item: message, attribute: .top, relatedBy: .equal, toItem: navController.navigationBar, attribute: .bottom, multiplier: 1, constant: 0)
    //apply constraints
    message.addConstraint(height)
    navController.view.addSubview(message)
    navController.view.addConstraints([equalWidth, alignBottom])
这也可以通过使用来实现,但它的冗长可能有点过分了

否则,您可以使用一些很好的lib,使其更具声明性,并减少锅炉板的大小

我的“穷人解决方案”是将状态栏高度硬编码为20:

 var newY = self.tableView.contentOffset.y
 newY += self.navigationController!.navigationBar.frame.size.height
 newY += 20

必须是一个更好的方式。。。但是这似乎是可行的。

属性视图.frame能够捕获这样的变化。因此,对任何位置使用
view.frame.height
都可以防止重新定位

它在有或没有热点栏的情况下找到正确视图高度的方式:

//with hotspot bar
print (view.frame.height) //540.0   
//without hotspot bar   
print (view.frame.height) //560.0

//setting a navigation bar at top using height of view frame
let navigationBar : UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width:view.frame.width, height: view.frame.height/10))
    navigationBar.backgroundColor =  .gray
navigationBar.setItems([UINavigationItem(title: "navigaion bar")], animated: true)    

let statusbar = UITableView()
//placing the status bar below the navigation bar 
statusbar.frame = CGRect(x: 0, y: navigationBar.frame.size.height, width:view.frame.width, height: view.frame.height/10) //any choice for height:
view.addSubview(statusbar)
view.addSubview(navigationBar)

以前的代码不正确,我已经对示例进行了更新,使其成为工作集
definePresentationContext=true
我也有同样的问题..您在没有设置框架的情况下得到了答案吗?添加了一些详细信息。很高兴它有一点用处。