开发者

Labels not aligning properly in landscape mode

开发者 https://www.devze.com 2023-03-29 05:06 出处:网络
Is the code below correct? When the user rotates the device, two labels are supposed to go to the 开发者_JAVA百科coordinates given below. It works when the user starts the app in portrait mode, the la

Is the code below correct? When the user rotates the device, two labels are supposed to go to the 开发者_JAVA百科coordinates given below. It works when the user starts the app in portrait mode, the labels are placed correctly. However, when the user starts in landscape mode, the labels DO NOT get placed correctly. But if you rotate the view to portrait and then back to landscape, they align properly. I've tried placing the landscape coordinates in viewDidLoad, and it still doesn't work. What should I do? Thanks for your help!

The two labels are recordingTimeLabel and 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");

 }



}

Additionally, this code doesn't work either:

-(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");

}

}


willRotateToInterfaceOrientation: may not be called if you start in landscape mode. I suggest setting the coordinates in viewWillAppear:, not viewDidLoad, to figure out the initial orientation (you can use self.interfaceOrientation if you have autorotation enabled).


Have you set following code also for the UIInterfaceOrientation?

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


Got it. I used an NSTimer and called a function very often that contained this code:

-(void)updateLabelLocation {

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

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

    } 

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号