- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"%d",toInterfaceOrientation);
[self performSelectorOnMainThread:@selector(changeShelfPosition:) withObject:[NSNumber numberWithInt:toInterfaceOrientation] waitUntilDone:YES];
}
-(void)changeShelfPosition:(NSNumber *)orientation
{
if (orientation.integerValue == 1 ) {
self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 544, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
} else if (orientation.integerValue == 2) {
self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 544, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
} else if (orientation.integerValue == 3) {
self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 594, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
} else if (orientation.integerValue == 4) {
self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 594, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
}
}
- (void)viewDidLoad
{
...
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == 0) {
}//Default orientation
//UI is in Default (Portrait) -- this is really a just a failsafe.
else if(orientation == UIInterfaceOrientationPortrait) {
self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
}
//Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft) {
self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y+50, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
}
// Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight) {
self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y+50, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
}
}
答案 0 :(得分:0)
您无法访问viewDidLoad
中的框架或其他几何属性,并且期望得到一致的结果,因为尚未设置视图几何体。
尝试使用viewWillAppear
。