2015-04-02 38 views
-1

我想在單擊單元格時添加一個選項,它將在鍵盤上方顯示浮動文本框並使背景變模糊。在ios應用程序中創建浮動文本框

任何人都知道這一點,以及如何實施它?與添加一些屬性類 Floating Text Box

+0

論索引路徑沒有選擇行你會想使用UITextField創建一個新視圖,然後呈現視圖並使文本字段成爲第一響應者,您將希望根據觸發鍵盤時發送的NSNotification信息更新文本框的框架。爲了模糊背景,您可以使用iOS 8(如果您使用它)?模糊背景圖層,可以將UITextField添加到它的活力層,以確保前景色鮮明。 - 您的屏幕截圖看起來非常像黑色層,大約80%的不透明度 - 您可以直接在UIView上做到這一點。 – 2015-04-02 13:59:31

回答

2

開始: 您可以通過下面的鏈接查看圖像

var textField = UITextField() 
var composeBarView = UIView() 
var blurView = UIView() 
let barHeight:CGFloat = 44 

下創建的模糊觀點,文本框和容器視圖。在viewWillAppear中做到這一點:

blurView = UIView(frame: self.view.frame) 
blurView.alpha = 0.5 
blurView.backgroundColor = UIColor.blackColor(); 
self.view.addSubview(blurView); 
blurView.hidden = true;// Note its hidden! 

textField = UITextField(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, barHeight)) 
composeBarView = UIView(frame: CGRectMake(0, UIScreen.mainScreen().bounds.height-64, UIScreen.mainScreen().bounds.width, barHeight)); 
composeBarView.addSubview(textField); 

self.view.addSubView(composeBarView); 

那麼你應該爲UIKeyboardWillShowNotification和UIKeyboardWillHideNotification註冊通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillToggle:", name: UIKeyboardWillShowNotification, object: nil); 
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillToggle:", name: UIKeyboardWillHideNotification, object: nil); 

落實keyboardWillToggle方法:

func keyboardWillToggle(notfication: NSNotification){ 
    if let userInfo = notfication.userInfo { 

     let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue() 
     let startFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue(); 

     let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0 

     let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber 
     let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue 
     let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw) 


     var signCorrection:CGFloat = 1; 
     if (startFrame.origin.y < 0 || startFrame.origin.x < 0 || endFrame.origin.y < 0 || endFrame.origin.x < 0){ 
      signCorrection = -1; 
     } 

     let widthChange = (endFrame.origin.x - startFrame.origin.x) * signCorrection; 
     let heightChange = (endFrame.origin.y - startFrame.origin.y) * signCorrection; 

     let sizeChange = UIInterfaceOrientationIsLandscape(self.interfaceOrientation) ? widthChange : heightChange; 

     var frame = composeBarView.frame 

     frame.origin.y += (sizeChange - barHeight ?? 0.0) 
     composeBarView.frame = frame; 
     UIView.animateWithDuration(duration, 
      delay: NSTimeInterval(0), 
      options: animationCurve, 
      animations: { self.view.layoutIfNeeded() }, 
      completion: nil) 
    } 
} 

請注意,我們必須考慮酒吧高度,當你顯示鍵盤。您需要將視圖調整回原來的位置。 然後,當didSelectRowAtIndexPath方法是叫你撥打:

textFiled.becomeFirstResponder() 
blureView.hidden = false; 
0

沒有測試,但是我認爲你可以用一個模式賽格瑞辦呢?

對於爲例,當成爲編輯,你可以Segue公司到會出現實際泰伯維上述新CellViewcontroller和插入此參數的文本字段:

func prensentationController(controller: UIPresentationController, viewControllerdaptivePresentationStyle style: UIModalPresentationtyle) -> UIViewController 
    let navcon = UINavigationController(UIPresentationViewController: controller.presentedViewController) 
    let visualEffectView = UIVisualEffectView(effect:UIBlurEffect(style: .ExtraLight)) 
    visualEffectView.frame = navcon.view.bounds 
    navcon.view.insertSubview(visualEffectView, atIndex: 0) 
    return navcon 
}