2016-07-25 84 views
1

我想添加一個(自定義的圓角半徑爲25.0)的UIButton,將鼠標懸停在我的視圖控制器中的鍵盤上方。Swift:以編程方式在鍵盤上方添加UIButton

我曾嘗試但添加該按鈕,工具欄用下面的代碼,我自己完成這一點,我不是在尋找一個工具欄:

let toolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50)) 
    toolbar.barStyle = UIBarStyle.Default 
    loginButton.titleLabel?.textColor = UIColor.whiteColor() 
    loginButton.backgroundColor = UIColor.redColor() 
    loginButton.titleLabel?.text = "Sign Up" 
    loginButton.addTarget(self, action: #selector(signUp), forControlEvents: .TouchUpInside) 
    let customButton = UIBarButtonItem(customView: loginButton) 
    toolbar.items = [customButton] 
    toolbar.sizeToFit() 
    //... 
    //When credentials are valid, show/enable button... 
    usernameEmailField.inputAccessoryView = toolbar 

我如何添加一個UIButton徘徊一直在鍵盤上方?

謝謝。

+0

您應該瞭解如何添加鍵盤配件。以下鏈接可能會有所幫助:https://developer.apple.com/library/ios/samplecode/KeyboardAccessory/Introduction/Intro.html http://stackoverflow.com/questions/27573045/how-do-i-create- a-keyboard-accessory-view-with-auto-layout-in-interface-builder – iMuzahid

回答

0

這個爲我工作。請嘗試此操作

//create toolbar object 
     let doneToolBar: UIToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.size.width, height: 44)) 
     doneToolBar.barStyle = UIBarStyle.BlackTranslucent 
     //add barbuttonitems to toolbar 
     let flexsibleSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) // flexible space to add left end side 
     let doneButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(UITextField.didPressDoneButton)) 
     doneToolBar.items = [flexsibleSpace,doneButton] 
     //assing toolbar as inputAccessoryView 
     textField.inputAccessoryView = doneToolBar 
     doneToolBar.sizeToFit() 
+0

謝謝,但這已經是我剛剛在左側留下的空間了。你知道我怎麼可以添加一個自定義的UIButton,而不是UIBarButtonItem,到工具欄?我目前的做法是將自定義UIButton放到工具欄中,但刪除了角RadRad並且大小不合適: 'let customButton = UIBarButtonItem(customView:loginButton)''toolbar.items = [customButton] – DiligentDev

相關問題