简体   繁体   English

如何在UIStackView内设置UIView的背景色?

[英]How to set background color of UIView inside of UIStackView?

I am making a Detail screen which has several nested views in the Storyboard View Controller: ScrollView > UIView > StackView > 5 UI Views. 我正在制作一个“详细信息”屏幕,在“故事板”视图控制器中具有多个嵌套视图:ScrollView> UIView> StackView> 5个UI Views。 Each UI view is a different section that contains labels and edit texts. 每个UI视图都是一个包含标签和编辑文本的不同部分。

When I set the background of one of the UIViews in the storyboard it changes but does not reflect this change when I run the emulator or on an iPad. 当我在情节提要中设置一个UIView的背景时,它会更改,但在运行模拟器或在iPad上时不会反映此更改。

Below is a picture with the emulator view on the left and the Storyboard in Xcode on the right. 下图是左侧的仿真器视图,右侧是Xcode的Storyboard的图片。 You can see that the emulator shows no background color on the nested UIView while the Storyboard does. 您可以看到,情节提要时模拟器在嵌套的UIView上没有显示背景色。

Emulator vs Storyboard 模拟器与情节提要

Because it was asked for here is the full Storyboard. 因为这里要求提供完整的故事板。 It is tough to see but it is a tab view controller connected to three split view controllers. 很难看清,但它是一个连接到三个拆分视图控制器的选项卡视图控制器。

Full Storyboard 完整故事板

By testing with only specific views I have narrowed it down to the UIStackView holding the 5 UIViews that causes the problem. 通过仅使用特定视图进行测试,我将其范围缩小到包含导致问题的5个UIView的UIStackView。 I have tried attaching a controller and changing the color in code but nothing I've tried seems to change it. 我试图附加一个控制器并更改代码中的颜色,但是我尝试过的任何事情似乎都没有改变它。

class ClientDetailViewController: UIViewController {

@IBOutlet weak var basicInfoUIView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    basicInfoUIView.backgroundColor = UIColor.init(displayP3Red: 22, green: 22, blue: 22, alpha: 0.5)
    }
}

I expect for each section (UIView) to be a dark gray color but instead they are all clear so the scrollview background color can be seen. 我希望每个部分(UIView)都是深灰色,但是它们都是透明的,因此可以看到scrollview背景色。

UIStackView doesn't allow changing background color: UIStackView不允许更改背景颜色:

The UIStackView is a nonrendering subclass of UIView; UIStackView是UIView的非呈现子类。 that is, it does not provide any user interface of its own. 也就是说,它不提供自己的任何用户界面。 Instead, it just manages the position and size of its arranged views. 相反,它仅管理其排列视图的位置和大小。 As a result, some properties (like backgroundColor) have no effect on the stack view. 结果,某些属性(如backgroundColor)对堆栈视图没有影响。 Similarly, you cannot override layerClass, draw( :), or draw( :in:). 同样,您不能覆盖layerClass,draw( :)或draw( :in :)。

(from UIStackView ) (来自UIStackView

A simple workaround is to add another UIView behind the stack view, setup the constraints to match the position and size of the stack view (equal top, left, right, bottom) and change background color of that view. 一个简单的解决方法是在堆栈视图后面添加另一个UIView ,设置约束以匹配堆栈视图的位置和大小(相等的顶部,左侧,右侧,底部),并更改该视图的背景颜色。

在此处输入图片说明 I think first you should give different background colors to your ui views then place them. 我认为您首先应为ui视图赋予不同的背景颜色,然后再放置它们。 Under a single stack and check whether it is working or not 在单个堆栈下,检查其是否正常工作

I have added a screenshot where i first gave background colors to UIviews and then placed them under one stackView but they had those colors unlike you after giving them background color simply from the uielements under the controller on the left pick the Uiviews and place them under stack View 我添加了一个屏幕快照,在该屏幕截图中,我先给UIviews提供了背景色,然后将它们放在一个stackView下,但是在您从左侧控制器下方的uielements中给它们提供背景色之后,它们具有与您不同的那些颜色,请选择Uiviews并将它们放在堆栈下视图

Well, check your color please. 好吧,请检查你的颜色。 22 should not belong to [0,1]. 22不应该属于[0,1]。

basicInfoUIView.backgroundColor = UIColor.init(displayP3Red: 22.0 / 255.0, green: 22.0 / 255.0, blue: 22.0 / 255.0, alpha: 0.5)

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

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