ios - 自定义 UIView 不显示

标签 ios objective-c cocoa-touch uiview

我有一个非常简单的 UIView 来创建盒子,但是 UIView 根本没有显示,这是我在 sharingButtons.m

上的代码
-(void)createContainer{

winWidth = [UIScreen mainScreen].bounds.size.width;


buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 20)];
buttonContainer.backgroundColor = [UIColor redColor];


[self.view addSubview:buttonContainer];

 }


-(void)createButton{
[self createContainer];
}

这是我的sharingButtons.h

 @interface SocialSharing : UIViewController {
int winWidth;
 }

- (void)createButton;
- (void)createContainer;

#pragma mark - Properties

@property(nonatomic, strong) UIView* buttonContainer;

@end

并且 createButton 方法在 viewDidLoad 上从 MyViewControler.m 调用

我的代码有问题吗??

已编辑

这是我在 MyViewControler.m 上的代码

- (void)loadSocialSharingButton {

socialButtons = [[SocialSharing alloc] init];

[socialButtons createButton];
}

- (void)viewDidLoad {

[super viewDidLoad];

[self loadSocialSharingButton];

}

抱歉,我刚刚了解 obj c :)

非常感谢

最佳答案

您的 buttonContainer 不可见的原因是,它没有加载到您的 View 层次结构中。

要使其可见,您应该将其添加为 subview 。在 viewDidLoadMyViewController.m 中,在 [self loadSocialSharingButton];

之后添加以下行
[self.view addSubview:socialButtons.buttonContainer];

希望对您有所帮助!

关于ios - 自定义 UIView 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30364452/

相关文章:

objective-c - 将 subview 添加到 UITabBarController

iphone - 键盘显示然后在显示 MFMessageComposeViewController 时立即隐藏自己

ios - 如何从不同的 ViewController 执行 collectionView 类

objective-c - 更改 -[NSArray sortedArrayUsingComparator :] 的排序顺序

iphone - Cocoa 错误 256,当使用 initWithContentsOfURL :

objective-c - 解析和排序需要立即显示的大型 NSMutableArray

iphone - 需要 Objective-C 设计帮助,可能需要协议(protocol)?

ios - 在 IOS Swift 中将音频文件作为附件通过电子邮件发送不会附加文件

ios - customTableViewCell 不工作

注册远程通知时 iOS7 崩溃