【问题标题】:Animate the height of inputAccessoryView Swift为 inputAccessoryView Swift 的高度设置动画
【发布时间】:2015-03-05 06:48:35
【问题描述】:

您好,我试图在单击后更改设置为 inputAccessoryView 的按钮的高度。

不幸的是,它似乎只改变了位置而不改变高度。

谁能帮帮我?在我的代码下方。

谢谢!

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {



 @IBOutlet var textField: UITextField!

    var SendButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

    override func viewDidLoad() {
        super.viewDidLoad()

        SendButton.frame = CGRectMake(0, 0, UIScreen.mainScreen().applicationFrame.width, 30)
        SendButton.backgroundColor = UIColor(red: 184/255.0, green: 56/255.0, blue: 56/255.0, alpha: 1)
        SendButton.setTitle("Send", forState: .Normal)
        SendButton.addTarget(self, action: "sendNotification", forControlEvents: UIControlEvents.AllEvents)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func sendNotification(){

        UIView.animateWithDuration(1, animations: {
            self.SendButton.frame = CGRectMake(0, -340, UIScreen.mainScreen().applicationFrame.width, 30)
            self.SendButton.backgroundColor = UIColor(red: 300/255.0, green: 56/255.0, blue: 56/255.0, alpha: 1)
            self.SendButton.setTitle("Hello", forState: .Normal)

        })
    }


    func textFieldDidBeginEditing(textField: UITextField) {
        textField.inputAccessoryView = self.SendButton
    }

}

【问题讨论】:

    标签: ios swift keyboard inputaccessoryview


    【解决方案1】:

    在您的动画块中,您正在更改 CGRectMake 中框架的 y 位置,而不是高度。这就是为什么位置在变化而不是高度的原因。

     UIView.animateWithDuration(1, animations: {
     // Your problem is the line below
            self.SendButton.frame = CGRectMake(0, -340, UIScreen.mainScreen().applicationFrame.width, 30) 
        })
    

    函数CGRectMake的函数参数为
    CGRectMake(xPosition, yPosition, width, height)

    将下面的代码更改为您想要的高度。

       UIView.animateWithDuration(1, animations: {
            self.SendButton.frame = CGRectMake(0, 0, UIScreen.mainScreen().applicationFrame.width, yourDesiredHeight) 
        })
    

    但这无助于更改输入附件的高度。

    首先添加一个单独的UIView 作为输入附件(增加了高度)并将按钮添加到这个UIView 中。之后为里面的按钮设置动画。这行得通。 您可以查看 gist 中的代码
    https://gist.github.com/rakeshbs/539827925df923e41afe

    【讨论】:

    • 您好,感谢您的回复。但不幸的是,即使改变高度也不起作用。高度仍然保持不变。你知道为什么吗?谢谢
    • 我已经解决了这个问题,方法是使用另一个增加了高度的视图作为 inputaccessoryview 并将按钮添加到其中。检查代码。 gist.github.com/rakeshbs/539827925df923e41afe
    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多