ios Swift - 将标签设置在其他视图项之上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32478758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:34:16  来源:igfitidea点击:

Swift - Set label to be on top of other view items

iosswift

提问by tonyedwardspz

I'm creating a basic sketching app using swift 1.2 and Xcode 6.4. The feature I'm working on is adding text to the image for future manipulation.

我正在使用 swift 1.2 和 Xcode 6.4 创建一个基本的草图应用程序。我正在研究的功能是向图像添加文本以供将来操作。

So far I have the text the user enters adding to the drawing via label, but it is being placed (I assume) behind the UIImagethat is to be drawn upon. This is preventing the associated tap gesture from being recognised. The label is also obscured when drawn over.

到目前为止,我已经将用户输入的文本通过标签添加到绘图中,但它被放置(我假设)在UIImage要绘制的后面。这会阻止相关联的点击手势被识别。标签在绘制时也会被遮挡。

How do I place the label on top of everything else to prevent it being drawn over? The end goal is to allow the user to move the label.

如何将标签放在其他所有内容的顶部以防止其被覆盖?最终目标是允许用户移动标签。

If I was using my usual web stack I'd set the z-indexof the element to a high value via CSS.

如果我使用我常用的网络堆栈,我会z-index通过 CSS 将元素的 设置为高值。

Edit: Using view.bringSubviewToFront(label)prevents the text from being drawn over, but does not allow the tap gesture to be picked up.

编辑:使用view.bringSubviewToFront(label)可防止文本被绘制,但不允许拾取点击手势。

Heres my code:

这是我的代码:

    func printText(){
        println(textString)

        var label = UILabel(frame: CGRectMake(5, 100, 200, 50 ))
        label.textAlignment = NSTextAlignment.Center
        label.text = textString;

        let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap"))
        label.addGestureRecognizer(tap)

        self.view.addSubview(label);
    }

    func handleTap() {
        print("tap working")
    }

Any help much appreciated.

非常感谢任何帮助。

回答by Miknash

If you want z-index use:

如果你想要 z-index 使用:

label.layer.zPosition = 1;

Also you could play with bringToFront method on the parent view:

你也可以在父视图上使用 combineToFront 方法:

self.view.bringSubviewToFront(label);

You can check that function here : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/

您可以在此处检查该功能:https: //developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/

Also, you should add the following:

此外,您应该添加以下内容:

label.userInteractionEnabled = true

to enable touches.

启用触摸。

回答by Rafal

Try using those in the parent view:

尝试在父视图中使用它们:

view.bringSubviewToFront(label)
view.sendSubviewToBack(imageView)

It all depends on what you can, or want to do.

这一切都取决于你能做什么,或者想要做什么。

回答by simardeep singh

You can call label to front by

您可以通过以下方式调用标签到前面

self.view.bringSubviewToFront(label)

or

或者

You can send image view to the back by

您可以通过以下方式将图像视图发送到后面

self.view.sendSubviewToBack(imageView)