对inputAccessoryView Swift的高度进行动画处理 [英] Animate the height of inputAccessoryView Swift

查看:94
本文介绍了对inputAccessoryView Swift的高度进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我尝试更改设置为inputAccessoryView的按钮的高度.

Hello I' trying to change the height of a button set as inputAccessoryView once I click on it.

不幸的是,似乎只能更改位置,而不能更改高度.

Unfortunately it seems only to change the position but not the height.

有人可以帮我吗?在我的代码下面.

Can Anyone help me out? Below my code.

谢谢!

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
    }

}

推荐答案

在动画块中,您正在CGRectMake中更改帧的y方向,而不是高度.这就是位置不断变化而不是高度变化的原因.

In your animation block you are changing the y posistion of the frame in CGRectMake rather than the height. that's why the position is changing and not the height.

 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)

The function parameters of the function CGRectMake are
CGRectMake(xPosition, yPosition, width, height)

将下面的代码更改为所需的高度.

Change the code below to your desired height.

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

但这在设置输入附件后不能帮助改变其高度.

But this doesn't help in changing the height of the input accessory once it was set.

首先添加一个单独的UIView作为输入附件(具有增加的高度),然后将按钮添加到该UIView中.之后,对内部的按钮进行动画处理.这行得通. 您可以在要旨中查看代码
https://gist.github.com/rakeshbs/539827925df923e41afe

First add a separate UIView as input accessory(with added height) and add the button into this UIView. Animate the button inside after that. This works. You can check out the code in the gist
https://gist.github.com/rakeshbs/539827925df923e41afe

这篇关于对inputAccessoryView Swift的高度进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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