Ios 如果在viewDidLoad中添加为子视图,则不会渲染UIView
我试图通过编程方式将子视图添加到视图控制器中,但如果我在视图控制器的Ios 如果在viewDidLoad中添加为子视图,则不会渲染UIView,ios,objective-c,uiview,ios7,viewdidload,Ios,Objective C,Uiview,Ios7,Viewdidload,我试图通过编程方式将子视图添加到视图控制器中,但如果我在视图控制器的viewDidLoad中添加子视图,则视图控制器中不会呈现我在视图中添加为子视图的内容 但是,如果我在视图控制器的init方法中移动[self.view addSubview:myUIView],所有内容都会呈现 另外,如果我在viewdideappear中调用相同的方法,我会渲染所有元素,但就在视图控制器显示之后,我可以看到元素在视图控制器中渲染的时间 这将是我的视图控制器: //interface @class RLJSig
viewDidLoad
中添加子视图,则视图控制器中不会呈现我在视图中添加为子视图的内容
但是,如果我在视图控制器的init
方法中移动[self.view addSubview:myUIView]
,所有内容都会呈现
另外,如果我在viewdideappear
中调用相同的方法,我会渲染所有元素,但就在视图控制器显示之后,我可以看到元素在视图控制器中渲染的时间
这将是我的视图控制器:
//interface
@class RLJSignInView;
@protocol RLJSignInViewControllerDelegate;
@interface RLJSignInViewController : UIViewController <UITextFieldDelegate>
@property (nonatomic, readwrite) RLJSignInView *signInView;
@property (nonatomic, assign) id<RLJSignInViewControllerDelegate> delegate;
@end
//implementation
#import "RLJSignInViewController.h"
#import "RLJSignInView.h"
#import "RLJSignInViewControllerDelegate.h"
#import "UIColor+RLJUIColorAdditions.h"
@implementation RLJSignInViewController
- (id)init
{
self = [super init];
if (self) {
_signInView = [[RLJSignInView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.signInView];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@", self.signInView);
[self.view setBackgroundColor:[UIColor rgbWithRed:250 green:250 blue:250]];
}
@end
我不确定我能做些什么,但我确定我不希望在初始化时将视图添加到子视图中
或者,我对视图的子视图进行了错误的渲染。如果有人遇到同样的事情,或者注意到我把事情弄糟了,我将非常感谢您在这件事上提供一些信息。不要调用
self。从init
方法内部查看。这将触发在init
方法返回之前调用viewDidLoad
,这是不正确的行为
相反,请遵循以下模式:
- 在
init
中创建对象(属性/实例变量),或使用惰性实例化
- 在
viewDidLoad
- 在
viewWillLayoutSubviews
此模式将避免您在此处发布的错误,并为您将来成功处理旋转和调整大小事件做好准备。不要从init
方法内部调用self.view
。这将触发在init
方法返回之前调用viewDidLoad
,这是不正确的行为
相反,请遵循以下模式:
- 在
init
中创建对象(属性/实例变量),或使用惰性实例化
- 在
viewDidLoad
- 在
viewWillLayoutSubviews
此模式将避免您在此处发布的错误,并为您将来成功处理旋转和调整大小事件做好准备。不要从init
方法内部调用self.view
。这将触发在init
方法返回之前调用viewDidLoad
,这是不正确的行为
相反,请遵循以下模式:
- 在
init
中创建对象(属性/实例变量),或使用惰性实例化
- 在
viewDidLoad
- 在
viewWillLayoutSubviews
此模式将避免您在此处发布的错误,并为您将来成功处理旋转和调整大小事件做好准备。不要从init
方法内部调用self.view
。这将触发在init
方法返回之前调用viewDidLoad
,这是不正确的行为
相反,请遵循以下模式:
- 在
init
中创建对象(属性/实例变量),或使用惰性实例化
- 在
viewDidLoad
- 在
viewWillLayoutSubviews
此模式将避免您在此处发布的错误,并为您将来成功处理旋转和调整大小事件做好准备。关键可能是您使用self.view.bounds太早了。当您提供一个初始大小的帧时(像IB一样),它可能会起作用
init
太早了。视图控制器正在初始化,视图尚未加载,这意味着没有视图,边界将返回CGRectZero
viewDidLoad
依赖视图的边界也为时过早。视图刚刚加载,它还没有superview,以后可能会调整大小
视图将出现
是您可以依赖视图边界的第一个时刻。因为它将根据AutoResizingMask或autolayout约束调整大小
您可以在viewdiload
中添加子视图,但您应该提供一个初始帧(并设置正确的自动重新设置网格或约束),它应该可以正常工作。关键可能是您使用self.view.bounds太早了。当您提供一个初始大小的帧时(像IB一样),它可能会起作用
init
太早了。视图控制器正在初始化,视图尚未加载,这意味着没有视图,边界将返回CGRectZero
viewDidLoad
依赖视图的边界也为时过早。视图刚刚加载,它还没有superview,以后可能会调整大小
视图将出现
是您可以依赖视图边界的第一个时刻。因为它将根据AutoResizingMask或autolayout约束调整大小
您可以在viewdiload
中添加子视图,但您应该提供一个初始帧(并设置正确的自动重新设置网格或约束),它应该可以正常工作。关键可能是您使用self.view.bounds太早了。当您提供一个初始大小的帧时(像IB一样),它可能会起作用
init
太早了。视图控制器正在初始化,视图尚未加载,这意味着没有视图,边界将返回CGRectZero
viewDidLoad
依赖视图的边界也为时过早。视图刚刚加载,它还没有superview,以后可能会调整大小
视图将出现
是您可以依赖视图边界的第一个时刻。
//interface
@interface RLJSignInView : UIView
@property (strong, nonatomic, readwrite) UITextField *username;
@property (strong, nonatomic, readwrite) UITextField *password;
@property (strong, nonatomic, readwrite) UIButton *signIn;
@property (strong, nonatomic, readwrite) UIButton *signUp;
@end
//implementation
#import "RLJSignInView.h"
#import "UITextField+RLJUITextFieldAdditions.h"
#import "UIButton+RLJUIButtonAdditions.h"
@implementation RLJSignInView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect usernameInputFrame = CGRectMake(10.0, 40.0, self.bounds.size.width - 20, 40);
CGRect passwordInputFrame = CGRectMake(10.0, 79.0, self.bounds.size.width - 20, 40);
CGRect signInButtonFrame = CGRectMake(10.0, 160, self.bounds.size.width - 20, 40);
CGRect signUpButtonFrame = CGRectMake(10.0, 220, self.bounds.size.width - 20, 40);
UIView *usernameInputLeftView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10.0, 40)];
UIView *passwordInputLeftView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 10.0, 40)];
self.username = [[UITextField alloc] initWithFrame:usernameInputFrame
textAlignment:NSTextAlignmentLeft
textColor:[UIColor blackColor]
clearButton:UITextFieldViewModeWhileEditing
leftView:usernameInputLeftView
placeholder:@"Username"
backgroundColor:[UIColor whiteColor]
strokeWidth:2.0
strokeColor:[UIColor lightGrayColor]
keyboardType:UIKeyboardTypeEmailAddress
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(4.0, 4.0)
secure:NO];
self.password = [[UITextField alloc] initWithFrame:passwordInputFrame
textAlignment:NSTextAlignmentLeft
textColor:[UIColor blackColor]
clearButton:UITextFieldViewModeWhileEditing
leftView:passwordInputLeftView
placeholder:@"Password"
backgroundColor:[UIColor whiteColor]
strokeWidth:2.0
strokeColor:[UIColor lightGrayColor]
keyboardType:UIKeyboardTypeDefault
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
cornerRadii:CGSizeMake(4.0, 4.0)
secure:YES];
self.signIn = [[UIButton alloc] initWithFrame:signInButtonFrame
title:@"Sign In"
colorNormal:[UIColor whiteColor]
colorHighlighted:[UIColor whiteColor]
colorDisabled:[UIColor whiteColor]
backgroundNormal:[UIColor colorWithRed:82 / 255.0 green:156 / 255.0 blue:201 / 255.0 alpha:1.0]
cornerRadius:4.0];
self.signUp = [[UIButton alloc] initWithFrame:signUpButtonFrame
title:@"Sign Up"
colorNormal:[UIColor blackColor]
colorHighlighted:[UIColor blackColor]
colorDisabled:[UIColor blackColor]
backgroundNormal:[UIColor whiteColor]
cornerRadius:4.0];
[self addSubview:self.username];
[self addSubview:self.password];
[self addSubview:self.signIn];
[self addSubview:self.signUp];
}
return self;
}
@end