简体   繁体   English

Xcode Storyboard Preview 不显示由代码添加的背景颜色

[英]Xcode Storyboard Preview doesn't show Background Color added by code

I changed the background color of View to a dark grey color in ViewController.swift and this shows up in the simulator but in the storyboard the View has a white background.我在 ViewController.swift 中将View的背景颜色更改为深灰色,这显示在模拟器中,但在故事板中, View具有白色背景。 Is there a way to refresh/force the storyboard to show the right background color.有没有办法刷新/强制故事板显示正确的背景颜色。

self.view.backgroundColor = UIColor(red:84.0/255.0, green:84.0/255.0, blue:84.0/255.0, alpha:1.0)

xcode 故事板预览并不能准确显示应用程序中将运行的内容,因此不幸的是,您的情况没有办法但是如果您想查看,您只需要更改故事板中的颜色或忽略它,因为代码将起作用

If you want to have black for example instead of white in the storyboard, you can do something like this:例如,如果您想在故事板中使用黑色而不是白色,您可以执行以下操作:

1.Create class ColorBlockView that will be @IBDesignable 1.创建类 ColorBlockView 将是@IBDesignable

@IBDesignable class ColorBlockView: UIView {
    @IBInspectable var blockColor: UIColor = UIColor.black {
        didSet{
            backgroundColor = blockColor
        }
    }
}

2. Then in Identity inspector choose ColorBlockView for the view that you want to change the background color 2. 然后在身份检查器中为要更改背景颜色的视图选择 ColorBlockView

在此处输入图片说明

3. Choose whatever color you want to have as a background 3. 选择你想要作为背景的任何颜色

在此处输入图片说明

4. If it didn't show the color, Editor -> Refresh All Views 4.如果没有显示颜色,编辑器->刷新所有视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM