未在视图上加载的UIImage在iOS中加载

时间:2013-09-02 07:55:10

标签: iphone ios uibutton uiimage

当我加载应用程序时,按钮图像未加载到viewDidLoad上的第一个视图中,但会在viewWillAppear之后加载。

这是我的代码,

if (activityorientation == UIInterfaceOrientationPortrait || activityorientation == UIInterfaceOrientationPortraitUpsideDown)
{
    CGRect frameloginscreenbutton1 = CGRectMake(0, 0, 768,1004);
    [loginscreenbutton1 setFrame:frameloginscreenbutton1];

}
else if(activityorientation == UIInterfaceOrientationLandscapeLeft || activityorientation == UIInterfaceOrientationLandscapeRight)
{       
    CGRect frameloginscreenbutton1 = CGRectMake(0, 0, 1024,768);
    [loginscreenbutton1 setFrame:frameloginscreenbutton1];
}

任何指针?

2 个答案:

答案 0 :(得分:1)

这取决于是否从笔尖加载按钮。如果从nib加载,则在调用viewDidLoad时它不存在。您可以使用断点自行验证。到viewDidAppear:被调用时,它已被创建。

如果要配置从Interface Builder加载的项目(无论是来自xib文件还是Storyboard),那么正确的时间是覆盖awakeFromNib

答案 1 :(得分:0)

我认为因为没有创建是或类似的东西......

您可以在零延迟后使用此方法在viewdidload中设置帧。

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelector:@selector(setButtonFrame) withObject:nil afterDelay:0];
}

-(void) setButtonFrame 
{
   //your code
}

如果它不起作用,请尝试其他延迟,例如0.1