2015-01-07 62 views
0

你好我试图改变一个按钮的高度设置为inputAccessoryView一旦我点击它。动画inputAccessoryView的高度Swift

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

任何人都可以帮我吗?在我的代码下面。

谢谢!

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

在你的动画块中,你正在改变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

+0

您好,感谢您的回复。但不幸的是,即使改变高度也无济于事。高度仍然保持不变。你有什么想法,为什么?谢谢 –

+0

你可以在 – rakeshbs

+0

这个问题上更新你当前的代码我已经通过使用另一个视图添加了高度作为inputaccessoryview并添加了按钮来解决问题。检查代码。 https://gist.github.com/rakeshbs/539827925df923e41afe – rakeshbs