标签在横向模式下无法正确对齐

时间:2011-08-20 06:41:33

标签: iphone ios ipad orientation coordinates

以下代码是否正确?当用户旋转设备时,两个标签应该转到下面给出的坐标。当用户以纵向模式启动应用程序时,它可以正常放置标签。但是,当用户以横向模式启动时,标签不会正确放置。但是,如果将视图旋转为纵向然后再返回横向,则它们会正确对齐。我已经尝试将景观坐标放在viewDidLoad中,但它仍然不起作用。我该怎么办?谢谢你的帮助!

这两个标签是recordingTimeLabel和recordingTimeLabelMinutes。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

 if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
     //is landscape

     backGround.frame = CGRectMake(0, 0, 768, 1024);

     recordingTimeLabel.center = CGPointMake(967, 22);
     recordingTimeLabelMinutes.center = CGPointMake(901, 22);

     NSLog(@"is landscape");

     //  fixedSpace.width = 400;

 } else {
     //is portrait

     backGround.frame = CGRectMake(0, 0, 1024, 768);

     recordingTimeLabel.center = CGPointMake(710, 22);
     recordingTimeLabelMinutes.center = CGPointMake(661, 22);

     NSLog(@"is portrait");

 }



}

此外,此代码也不起作用:

-(void)viewWillAppear:(BOOL)animated {        
if (([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) {
    //is landscape

    backGround.frame = CGRectMake(0, 0, 768, 1024);

    recordingTimeLabel.center = CGPointMake(967, 22);
    recordingTimeLabelMinutes.center = CGPointMake(901, 22);

    NSLog(@"is landscape");

} else {
    //is portrait

    backGround.frame = CGRectMake(0, 0, 1024, 768);

    recordingTimeLabel.center = CGPointMake(710, 22);
    recordingTimeLabelMinutes.center = CGPointMake(661, 22);

    NSLog(@"is portrait");

}

}

3 个答案:

答案 0 :(得分:1)

如果您以横向模式启动,则可能无法调用{p> willRotateToInterfaceOrientation:。我建议在viewWillAppear:而不是viewDidLoad设置坐标,以确定初始方向(如果启用了自动旋转,则可以使用self.interfaceOrientation。)

答案 1 :(得分:0)

您是否也为UIInterfaceOrientation设置了以下代码?

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    {
        return YES;
    }

答案 2 :(得分:0)

知道了。我使用了NSTimer并经常调用包含此代码的函数:

-(void)updateLabelLocation {

    if (([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) {

    recordingTimeLabel.center = CGPointMake(710, 22);
    recordingTimeLabelMinutes.center = CGPointMake(661, 22);

    } 

}