简体   繁体   English

尝试更改背景颜色时不显示自定义视图

[英]Custom view not displaying when attempting to change background color

This is my first mac application. 这是我的第一个Mac应用程序。 I've written for iOS before and have been able to accomplish this same goal. 我之前为iOS撰写过文章,并且能够实现相同的目标。

I'm trying to change the background color of a custom view. 我正在尝试更改自定义视图的背景颜色。 In the photo below, the two views are named view0 and view1 . 在下面的照片中,两个视图分别命名为view0view1

Here's the code I'm trying to use to accomplish this (in the AppDelegate.m file): 这是我试图用来完成此操作的代码(在AppDelegate.m文件中):

- (IBAction)upPressed:(id)sender {
    NSLog(@"upPressed");

    self.view0.layer.backgroundColor = CGColorCreateGenericRGB(1, 0, 0, 1);
}

I'd like the color of view0 to turn red upon pressing the up button. 我希望按向上按钮时view0的颜色变为红色。 The button click registers fine. 单击按钮可以正常注册。

When I run the simulator, the views do not even show up. 当我运行模拟器时,视图甚至都不会显示。

Upon clicking the up button, nothing changes. 单击向上按钮后,没有任何变化。

Why is this single line of code inadequate? 为什么这一行代码不足? In iOS, I was able to use a similar method of setting the background color of a view to simply make its color change. 在iOS中,我能够使用类似的方法来设置视图的背景颜色,以使其颜色发生简单变化。

在此处输入图片说明

Select the custom view and then select the 'Show View Effects Inspector' in the right pane (looks like 3 pieces of paper on top of each other in Utilities Window). 选择自定义视图,然后在右窗格中选择“显示视图效果检查器”(在“实用程序窗口”中看起来像3张纸一样)。 Check the box next to Custom View under Core Animation Layer. 选中“核心动画层”下“自定义视图”旁边的框。 This will enable the Core Animation for that view type. 这将为该视图类型启用核心动画。

Xcode中

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

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