Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ios应用程序中创建浮动文本框_Ios_Xcode_Swift - Fatal编程技术网

在ios应用程序中创建浮动文本框

在ios应用程序中创建浮动文本框,ios,xcode,swift,Ios,Xcode,Swift,我想添加一个选项,当点击一个单元格时,它会在键盘上方显示浮动文本框,并模糊背景 任何熟悉这一点的人以及如何实现它? 您可以通过以下链接查看图像: 首先向类中添加一些属性: var textField = UITextField() var composeBarView = UIView() var blurView = UIView() let barHeight:CGFloat = 44 接下来创建模糊视图、文本字段和容器视图。在视图中执行此操作将显示: blurView = UIView(

我想添加一个选项,当点击一个单元格时,它会在键盘上方显示浮动文本框,并模糊背景

任何熟悉这一点的人以及如何实现它? 您可以通过以下链接查看图像:

首先向类中添加一些属性:

var textField = UITextField()
var composeBarView = UIView()
var blurView = UIView()
let barHeight:CGFloat = 44
接下来创建模糊视图、文本字段和容器视图。在视图中执行此操作将显示:

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)
    }
}

还没有测试,但我认为你可以用模态序列来做

例如,当您的文本字段处于编辑状态时,您可以切换到新的CellViewcontroller,该控制器可以显示在实际的Tableview上方,并插入以下参数:

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
}

在did select row at index path上,您需要使用UITextField创建一个新视图,然后显示视图并使文本字段成为第一响应者,您需要根据键盘启动时发送的NSNOTITIONG信息更新文本字段的框架。要模糊背景,您可以使用iOS 8(如果您正在使用它?)模糊背景层,您可以将UITextField添加到其振动层中,以确保前景色充满活力。-你的屏幕截图,虽然它看起来真的像一个约80%不透明度的黑色层-你可以直接在UIView上这样做。
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
}