简体   繁体   English

如何将半透明的UIView添加到UIViewController

[英]How to Add a Semi Transparent UIView to a UIViewController

Upon a certain user action, I wish to add to my UIViewController another UIView that will be half transparent; 根据特定的用户操作,我希望将另一个半透明的UIView添加到我的UIViewController中。 ie when it loads, the UIViewController view in the back will still be visible in the background, and the new UIView will appear as a layer above it. 即,当它加载时,后面的UIViewController视图仍将在后台可见,并且新的UIView将在其上方显示为一层。

The "Half Transparent" UIView should have several images and buttons in it, so I prefer to create a separated h, m and xib files for it so I can control it. “半透明” UIView中应该包含多个图像和按钮,因此我更喜欢为其创建单独的h,m和xib文件,以便可以对其进行控制。

How should I do that? 我应该怎么做?

Try this: 尝试这个:

UIView *view = [[UIView alloc] init];
[view setAlpha:0.5];
[mainview addSubview:view]
  1. Subclass UIView, create the nib file 子类化UIView,创建nib文件
  2. Change the nib class to your custom subclass name 将笔尖类更改为您的自定义子类名称
  3. Change the file owner to become your view controller 更改文件所有者成为您的视图控制器
  4. In your view controller, declare a @property for the custom view using IBOutlet 在您的视图控制器中,使用IBOutlet为自定义视图声明一个@property
  5. Select the nib, drag from the file owner to the custom view and connect the outlet 选择笔尖,从文件所有者拖动到自定义视图并连接出口
  6. In your button action, when you are ready to load the view, use 在您准备好加载视图的按钮操作中,请使用

[[NSBundle mainBundle] loadNibNamed:@"NibName" owner:self options:nil];

Once this is done, your custom will be loaded from the nib and assigned to the property you declared. 完成此操作后,将从笔尖加载您的自定义并将其分配给您声明的属性。

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

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