2017-02-23 85 views
1

請幫忙! :)我是一個巨大的noobie在這裏。我從各種渠道收集了這些代碼,所以我不知道自己在做什麼。我的提醒控制器顯示a textfield that I can write in, a 'cancel' action, an 'ok' action,,它也顯示multiple 'input keyword to label' actions.如何滾動瀏覽警報控制器中的操作? Xcode 8,Swift 3,IOS

It has so many actions (around 20 keyword actions that i need) that it either doesn't fit in the screen, it is covered by the keyboard, or it is in the way of the keyboard.它看起來很醜,一些你不能觸摸的按鈕。有沒有辦法滾動瀏覽我的動作以使其適合這個空間?

這是我去年的圖像代碼:

//Quality Text Box (9) 

@IBOutlet weak var QualityLabel: UILabel! 

@IBAction func QualityTapped(_ sender: UIButton) { 
    print("Quality Button Tapped") 
    openQualityAlert() 
} 

func openQualityAlert() { 

//Create Alert Controller 
    let alert9 = UIAlertController (title: "Quality:", message: nil, preferredStyle: UIAlertControllerStyle.alert) 

//Create "Keyword -> label" actions 
    let bt1 = UIAlertAction(title: "Abuse", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Abuse"} 

    alert9.addAction(bt1) 

    let bt2 = UIAlertAction(title: "Hemmorhage", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Hemmorhage"} 

    alert9.addAction(bt2) 


    let bt3 = UIAlertAction(title: "Discomfort", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Discomfort"} 

    alert9.addAction(bt3) 

    let bt4 = UIAlertAction(title: "Acute", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Acute"} 
    alert9.addAction(bt4) 

    let bt5 = UIAlertAction(title: "Disease", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Disease"} 

    alert9.addAction(bt5) 

    let bt6 = UIAlertAction(title: "Rash", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Rash"} 

    alert9.addAction(bt6) 


    let bt7 = UIAlertAction(title: "Itch", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Itch"} 

    alert9.addAction(bt7) 

    let bt8 = UIAlertAction(title: "Burn", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Burn"} 
    alert9.addAction(bt8) 


    let bt9 = UIAlertAction(title: "Abuse", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Abuse"} 

    alert9.addAction(bt9) 

    let bt10 = UIAlertAction(title: "Hemmorhage", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Hemmorhage"} 

    alert9.addAction(bt10) 


    let bt11 = UIAlertAction(title: "Discomfort", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Discomfort"} 

    alert9.addAction(bt11) 

    let bt12 = UIAlertAction(title: "Acute", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Acute"} 
    alert9.addAction(bt12) 

    let bt13 = UIAlertAction(title: "Disease", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Disease"} 

    alert9.addAction(bt13) 

    let bt14 = UIAlertAction(title: "Rash", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Rash"} 

    alert9.addAction(bt14) 


    let bt15 = UIAlertAction(title: "Itch", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Itch"} 

    alert9.addAction(bt15) 

    let bt16 = UIAlertAction(title: "Burn", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Burn"} 
    alert9.addAction(bt16) 

    let bt17 = UIAlertAction(title: "Abuse", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Abuse"} 

    alert9.addAction(bt17) 

    let bt18 = UIAlertAction(title: "Hemmorhage", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Hemmorhage"} 

    alert9.addAction(bt18) 


    let bt19 = UIAlertAction(title: "Discomfort", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Discomfort"} 

    alert9.addAction(bt19) 

    let bt20 = UIAlertAction(title: "Acute", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Acute"} 
    alert9.addAction(bt20) 

    let bt21 = UIAlertAction(title: "Disease", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Disease"} 

    alert9.addAction(bt21) 

    let bt22 = UIAlertAction(title: "Rash", style: UIAlertActionStyle.default){ 
     (action) in self.QualityLabel.text = "Rash"} 

    alert9.addAction(bt22) 


    //Create Cancel Action 
    let cancel9 = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel, handler: nil) 

    alert9.addAction(cancel9) 

    //Create OK Action 
    let ok9 = UIAlertAction(title: "INPUT TEXTFIELD", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK") 
     let textfield = alert9.textFields?[0] 
     print(textfield?.text!) 
     self.QualityLabel.text = textfield?.text! 
    } 

    alert9.addAction(ok9) 

    //Add Text Field 
    alert9.addTextField { (textfield: UITextField) in 
     textfield.placeholder = "Quality" 
    } 

    //Present Alert Controller 
    self.present(alert9, animated:true, completion: nil) 
} 
+0

是的,你是對的,這是非常醜陋的,我想你再創建一個視圖控制器,文本字段設置爲視圖控制器的頂部,把下面的表格視圖放到文本框中,並將所有的按鈕文本設置到tableview單元格中,當點擊質量按鈕時出現視圖控制器,這對我來說是最好的選擇。 – Vivek

回答

1

雖然我找到了一種方法來滾動到UIAlertController行動的頂部,
我不建議這樣做,因爲我不知道你的應用程序是否會被拒絕。

我覺得這個答案只有適用於iOS相關10+

self.present(alert, animated: true, completion: { 
    //print(alert.view)            // _UIAlertControllerView 
    //print(alert.view.subviews)          // [UIView] 
    //print(alert.view.subviews[0].subviews)       // [_UIAlertControllerInterfaceActionGroupView] 
    //print(alert.view.subviews[0].subviews[0].subviews)    // [_UIDimmingKnockoutBackdropView, UIView] 
    //print(alert.view.subviews[0].subviews[0].subviews[0].subviews) // [UIView, UIVisualEffectView] 
    //print(alert.view.subviews[0].subviews[0].subviews[1].subviews) // [_UIInterfaceActionGroupHeaderScrollView, _UIInterfaceActionItemSeparatorView_iOS, _UIInterfaceActionRepresentationsSequenceView] 

    // _UIInterfaceActionRepresentationsSequenceView <- This is what we want 
    // In this view are all your added actions. It is a subview of UIScrollView, so just cast it and set the contentOffset to zero 

    if let scrollView = alert.view.subviews[0].subviews[0].subviews[1].subviews[2] as? UIScrollView { 
     scrollView.setContentOffset(.zero, animated: true) 
    } 
}) 

這可能不是某個工作,因爲我不檢查,如果子視圖是順序。
未來的iOS更新也可能完全崩潰此方法。
另外我只是在iPhone上測試了這個。我不知道它是否適用於iPad。

或多或少是安全的方式(建議表示歡迎):

self.present(alert, animated: true, completion: { 
    guard alert.view.subviews.count >= 1 else { return } 
    guard alert.view.subviews[0].subviews.count >= 1 else { return } 
    guard alert.view.subviews[0].subviews[0].subviews.count >= 2 else { return } 
    guard alert.view.subviews[0].subviews[0].subviews[1].subviews.count >= 3 else { return } 

    if let scrollView = alert.view.subviews[0].subviews[0].subviews[1].subviews[2] as? UIScrollView { 
     scrollView.setContentOffset(.zero, animated: true) 
    } 
}) 
-2

設置你的alertcontroller類與此代碼

https://gist.github.com/petrpavlik/0c5feaada5913cafde8a

+0

從下面的文檔中可以看出:'UIAlertController類是按原樣使用的,不支持子類。這個類的視圖層次是私有的,不能修改。' –

+0

也許我描述它是錯誤的。那個人的代碼有什麼問題? – DanielG