1

こんにちは、ボタンをクリックすると、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
    }

}
4

1 に答える 1

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でコードを確認できます。

于 2015-01-07T13:04:47.143 に答える