是否有一种简单的方法可以为 iOS 5.1 的 Xcode 中的视图添加边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11081886/
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-30 18:51:24  来源:igfitidea点击:

Is there an easy way to add a border to a view in Xcode for iOS 5.1

iosuiviewborder

提问by user1282180

I'm looking to have a border around a UIViewI have just to separate it from the main view visually.

我希望在 a 周围有一个边框,UIView我只需要在视觉上将它与主视图分开。

I have looked in the settings for the UIViewin storyboard editor but I can't seem to find anything for setting a border.

我查看了UIView情节提要编辑器的设置,但似乎找不到任何设置边框的内容。

Is there an easy way to do this in code?

有没有一种简单的方法可以在代码中做到这一点?

回答by mtisz

With Swift and XCode 6 you can do this.

使用 Swift 和 XCode 6,您可以做到这一点。

Click the UIView element in Storyboard, and go to identity inspector. In the user defined runtime attributes, enter:

单击 Storyboard 中的 UIView 元素,然后转到身份检查器。在用户定义的运行时属性中,输入:

layer.borderWidth number 1

If you want nice looking corners

如果你想要漂亮的角落

layer.cornerRadius number 5
layer.masksToBounds boolean true

Now this will give you a border but to set the colour you need to do it with code. Go to your view controller, and add an IBOutlet from your UIView. Say you do,

现在这会给你一个边框,但要设置颜色,你需要用代码来做。转到您的视图控制器,并从您的 UIView 添加一个 IBOutlet。说你做,

@IBOutlet weak var xView: UIView!

Call this in the viewDidLoad function like below to set the colour.

在 viewDidLoad 函数中调用它,如下所示来设置颜色。

xView.layer.borderColor = UIColor.whiteColor().CGColor

Thanks!

谢谢!

回答by seufagner

With this border, background still appears behind. In other words, the border isn't projected to outer of view, but to inner space.

有了这个边框,背景仍然出现在后面。换句话说,边界不是投影到视野外,而是投影到内部空间。

I think that is necessary create a overlay view behind the owner with size (width x height) increased with border size.

我认为有必要在所有者后面创建一个覆盖视图,其大小(宽度 x 高度)随着边框大小的增加而增加。