Iphone 将从nib加载的子视图添加到帧中
我想在单击按钮时将nib文件中的视图添加为主视图的子视图,但它必须位于帧中(0108320351)。我尝试了以下代码:Iphone 将从nib加载的子视图添加到帧中,iphone,uiview,Iphone,Uiview,我想在单击按钮时将nib文件中的视图添加为主视图的子视图,但它必须位于帧中(0108320351)。我尝试了以下代码: -(IBAction) displayJoinmeetup:(id)sender { [meetups removeFromSuperview]; //removing the older view CGRect myFrame = CGRectMake(0, 108,320,351); [joinmeetup initWithFrame:myFr
-(IBAction) displayJoinmeetup:(id)sender
{
[meetups removeFromSuperview]; //removing the older view
CGRect myFrame = CGRectMake(0, 108,320,351);
[joinmeetup initWithFrame:myFrame]; //joinmeetup declared in header file as IBoutlet UIView*joinmeetup
[self.view addSubview:joinmeetup];
}
这只显示一个空白屏幕,否则,如果我删除子视图显示的覆盖整个屏幕的框架,我如何才能正确执行此操作?对于从nib加载的视图,不会调用initWithFrame。有关详细信息,请参见UIView类参考。
您需要在interface builder(inspector)中设置视图大小属性。您可以按如下方式尝试:
-(IBAction) displayJoinmeetup:(id)sender
{
if(meetups)
{
[meetups removeFromSuperview]; //removing the older view
//also here you should release the meetups, if it is allocated
meetups = nil;
}
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 108,320,351)];
joinmeetup = tempView;
[tempView release];
[self.view addSubview:joinmeetup];
}
你不应该叫它init。。。在对象被创建后对其执行。如果您可以更改设置,则会有一个设置程序。在这种情况下,它是setFrame:
-(IBAction) displayJoinmeetup:(id)sender {
[meetups removeFromSuperview]; //removing the older view
CGRect myFrame = CGRectMake(0, 108,320,351);
[joinmeetup setFrame:myFrame]; //joinmeetup declared in header file as IBoutlet UIView*joinmeetup
[self.view addSubview:joinmeetup];
}
我建议你试试这个:
[joinmeetup setFrame:CGRectMake(0, 108,320,351)];
[self.view addSubview:joinmeetup];
框架是在UIView中声明的属性