2017-02-04 62 views
0

我試圖在iPhone上實現一個popovercontroller,它的模態表示類似於iPad。我只能在使用Storyboard時獲得UI,我更願意完全以編程方式構建控制器。 SO和谷歌上的所有演示似乎都使用故事板。我可以看到自定義UIViewController打印到日誌中的方法,但模式彈出窗口上沒有顯示任何方法。 Showing Popover view在iphone上創建只有自定義viewcontroller類的popoverview

func handlePop(sender: UIButton) { 

     //let popController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "unique") as UIViewController //only way I generate UI in popoverview 

     let popController = MyCustomViewController() //would like to use 

    //positioning 
    let placementView = UIView() 
    view.addSubview(placementView) 
    view.addConstraintsWithFormat("H:|[v0]|", views: placementView) 
    view.addConstraintsWithFormat("H:|-64-[v0(200)]", views: placementView) 

    popController.modalPresentationStyle = UIModalPresentationStyle.popover 
    if let poppingVC = popController.popoverPresentationController { 


     let popover: UIPopoverPresentationController = poppingVC 

     popover.sourceView = placementView 
     popover.permittedArrowDirections = .up 
     popover.sourceRect = CGRect(x: view.frame.width/2, y: -100, width: 0, height: 100) 

     popover.delegate = self 

     self.present(popController, animated: true, completion:nil) 
    } 

} 

func dismissView() { 
    self.dismiss(animated: true, completion: nil) 
} 
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { 
    return UIModalPresentationStyle.none 
} 

我缺少什麼是保持popovercontroller空白?

class RegionSelectionController: UIViewController, UIPopoverPresentationControllerDelegate, UITableViewDelegate, UITableViewDataSource { 



let mainLabel = UILabel() 
let cellId = "cellId" 
let footerId = "footerId" 
var keys = [String]() 

let tableView = UITableView() 


override func viewDidLoad() { 
    super.viewDidLoad() 


    tableView.delegate = self 
    tableView.dataSource = self 



    self.view.addSubview(tableView) 

    tableView.register(RegionFilterCell.self, forCellReuseIdentifier: cellId) 
    tableView.separatorStyle = .none 

    tableView.delegate = self 

} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print(keys.count) 
    return keys.count 

} 
func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! RegionFilterCell 

    let regionName = keys[indexPath.row] 
    cell.regionLabel.text = regionName 

    return cell 

} 

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { 
    let footerCell = RegionRequestNewLocationCell() 


    return footerCell 
} 
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return 100 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print(indexPath.row, keys[indexPath.row]) 
} 




override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    ref.child("Region").observeSingleEvent(of: .childAdded, with: { (snap) in 
     if snap.exists() { 

      let key: String = snap.key as String 
      self.keys.append(key) 
     } 
     DispatchQueue.main.async (execute: { 
      self.tableView.reloadData() 
     }) 
    }) 


} 

}

class RegionFilterCell: BaseTableCell { 

let regionLabel: UILabel = { 
    let label = UILabel() 
    label.font = UIFont(name: "SFUIText-Medium", size: 16) 
    label.text = "UMD" 
    return label 
}() 

let separatorBar: UIView = { 
    let view = UIView() 
    view.backgroundColor = UIColor.rgb(151, green: 151, blue: 151, alpha: 0.3) 
    return view 
}() 
override func setupViews() { 
    super.setupViews() 

    addSubview(regionLabel) 
    addSubview(separatorBar) 
    //addConstraintsWithFormat("H:[v0(100)]", views: regionLabel) 
    addConstraint(NSLayoutConstraint(item: regionLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)) 
    addConstraintsWithFormat("V:|[v0]|", views: regionLabel) 
    addConstraintsWithFormat("H:|[v0]|", views: separatorBar) 
    addConstraintsWithFormat("V:[v0(0.5)]|", views: separatorBar) 
} 

}

+0

你試過設置內容大小嗎? 'popController.preferredContentSize = CGSize(width:275,height:225)'。顯然,你想要改變任何你需要的h/w值 – Pierce

+0

添加'popController.preferredContentSize'只會改變popController的大小。你說我需要確保控制器的框架與' preferredContentSize'?仍然看到相同的模糊popview。是的,我看到你對可選展開的看法,但這是否阻止我的控制器顯示? – Josh

+0

不,對不起,我刪除了該評論,因爲它確實與您的問題無關。我真的不明白你是什麼問題。你有沒有嘗試將箭頭方向改爲'.any'? – Pierce

回答

0

從你正在展示,你永遠也設置在視圖控制器的內容真實呈現的畫面(在這種情況下,你UITableView) 。如果您沒有爲視圖設置框架,它將不會顯示在屏幕上。您需要使用CGRect指定原點和尺寸,或使用NSLayoutConstraints的自動佈局。最簡單的方法就是一個框架。因此,在viewDidLoad,說你想擁有的的tableView佔據整個視圖:

tableView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height) 

而且你需要確保你實例化正確的VC。我不知道你是否使用了名稱MyCustomViewController作爲示例,但是你在代碼中顯示的VC名爲RegionSelectionController

相關問題