简体   繁体   English

如何为UIView的背景设置颜色

[英]How to set color to UIView's background

I have set a custom gradient color background for my nav bar. 我为导航栏设置了自定义渐变颜色背景。 I want to set the same, matching color to some blank space I have at the bottom of my viewcontroller behind my UIButton. 我想为UIButton后面的viewcontroller底部的空白设置相同的匹配颜色。

This is the code I used for the navbar. 这是我用于导航栏的代码。 How can I use the exact same color and set it just for that small square section? 如何使用完全相同的颜色,并将其设置只是小正方形截面?

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:35/255.0 green:161.0/255.0 blue:202.0/255.0 alpha:1.0];

Though your question is not too clear, what I can gather from it is that you want a matching toolbar, similar to the navigationbar, behind a button at the bottom of a view. 尽管您的问题不太清楚,但是我可以从中得到的是,您需要一个匹配的工具栏,类似于导航栏,位于视图底部按钮的后面。

You can use the UIToolbar object: UIToolbar Documentation 您可以使用UIToolbar对象: UIToolbar文档

Treat it like you would treat any other view. 像对待其他视图一样对待它。

To add it to your view controller use the following code: 要将其添加到您的视图控制器,请使用以下代码:

CGFloat toolbarheight = 44.0f;

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - toolbarheight, self.view.frame.size.width, toolbarheight)];
toolbar.tintColor = [UIColor colorWithRed:35/255.0 green:161.0/255.0 blue:202.0/255.0 alpha:1.0];

[self.view addSubview:toolbar];
[toolbar release];

This should be done before you add your button as a subview. 这应该在将按钮添加为子视图之前完成。

You did mention that you wanted to use that color for a square section for which you would modify the frame of the toolbar on the init. 您确实提到过要在正方形截面上使用该颜色,为此您可以在初始化时修改工具栏的框架。

After this, it would be good practice to add the button as a subview of the toolbar so if you want to move the button and the toolbar to some other place on the view, you only have to worry about one frame (the toolbar frame). 此后,最好将按钮添加为工具栏的子视图,因此,如果要将按钮和工具栏移至视图上的其他位置,则只需担心一个框架(工具栏框架) 。

PS: The [toolbar release] call is made because adding a view as a subview increases it's retain count. PS:进行[工具栏发布]调用是因为将视图添加为子视图会增加它的保留计数。 To keep the retain count = 1 and avoid memory leaks, this is a best practice. 为了使保留计数= 1并避免内存泄漏,这是一种最佳实践。 You may have known this already but just throwing it in there. 您可能已经知道这一点,但只是将其扔在那里。

Again, your question wasn't too clear so I've answered it based on what I understood from it. 同样,您的问题不太清楚,因此我根据我的理解回答了该问题。

If this isn't the solution that you were looking for, maybe you could post some code or explain what you're trying to do so that we could help you better :) 如果这不是您想要的解决方案,也许您可​​以发布一些代码或解释您要做什么,以便我们可以更好地帮助您:)

** * **** EDIT * ** * *** ** * **** 编辑 * ** * ***

correct answer: 正确答案:

self.view.backgroundColor = [UIColor colorWithRed:35/255.0 green:161.0/255.0 blue:202.0/255.0 alpha:1.0];

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

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