动画inputAccessoryView Swift的高度

时间:2015-01-07 11:23:13

标签: ios swift keyboard inputaccessoryview

你好我试图在点击它之后改变设置为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
    }

}

1 个答案:

答案 0 :(得分: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中。之后为内部按钮设置动画。这有效。 你可以查看要点中的代码 https://gist.github.com/rakeshbs/539827925df923e41afe