如何滚动警报控制器中的操作? Xcode 8,Swift 3,IOS [英] How to scroll through actions in alert controller? Xcode 8, Swift 3, IOS

查看:261
本文介绍了如何滚动警报控制器中的操作? Xcode 8,Swift 3,IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙! :)我是一个巨大的noobie在这里。我从各种来源收集了这些代码,所以我真的不知道自己在做什么。我的警报控制器显示我可以写入的文本字段,'取消'动作,'确定'动作,它还显示多个'输入关键字来标记'操作。

Please help! :) Im a huge noobie here. I've gathered this code from various sources so i don't really know what I'm doing. My alert controller displays a textfield that I can write in, a 'cancel' action, an 'ok' action, and it also displays multiple 'input keyword to label' actions.

它有很多动作(我需要大约20个关键字动作)它要么不适合屏幕,要么被键盘覆盖,要么是键盘的方式。 它看起来很难看,还有一些你无法触摸的按钮。有没有办法滚动我的动作以使其适合这个空间?

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. It looks ugly and some of the buttons you cannot touch. Is there a way to scroll through my actions to make it fit within this space?

这是我最后一张图片的代码:

This is my code for that last image:

//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)
}


推荐答案

虽然我找到了一种滚动到UIAlertController动作顶部的方法,

我不建议这样做,因为我不知道您的应用是否会被拒绝。

Although I found a way to scroll to the top of UIAlertController actions,
I don't recommend doing this, because I don't know if your app would get rejected.

我认为这个答案仅适用于iOS 10 +

I think this answer is only relevant for 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。

This may not work sometime, because I don't check if the subviews are in this order.
A future iOS update could also completely crash this method.
Also I just tested this on an iPhone. I don't know if it works on an iPad.

或多或少的安全方式(建议欢迎):

A more or less safe way (suggestions welcome):

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)
    }
})

这篇关于如何滚动警报控制器中的操作? Xcode 8,Swift 3,IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆