开发者

iphone orientation not working

Hi i have a code which looks like this

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    // Custom initialization

    username = [[UILabel alloc]initWithFrame:CGRectMake(53,115, 83, 21)];
    username.text = @"Username";
    [username setFont:[UIFont fontWithName:@"Arial" size:14]];

    password = [[UILabel alloc]initWithFrame:CGRectMake(53,188, 75, 21)];
    password.text = @"Password";
    [password setFont:[UIFont fontWithName:@"Arial" size:14]];


    txtusername = [[UITextField alloc]initWithFrame:CGRectMake(42, 144, 233, 31)];
    [txtusername setBorderStyle:UITextBorderStyleRoundedRect];
   开发者_开发知识库 [txtusername setPlaceholder:@"Username"];
    [txtusername setAutocorrectionType:UITextAutocorrectionTypeNo];
    txtusername.delegate = self;
    txtusername.returnKeyType = UIReturnKeyDone;

    txtpassword = [[UITextField alloc]initWithFrame:CGRectMake(42, 217, 233, 31)];
    [txtpassword setBorderStyle:UITextBorderStyleRoundedRect];
    [txtpassword setPlaceholder:@"Password"];
    [txtpassword setAutocorrectionType:UITextAutocorrectionTypeNo];
    txtpassword.delegate = self;
    txtpassword.returnKeyType = UIReturnKeyDone;

    loginbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [loginbutton setFrame:CGRectMake(110, 269, 124, 26)];
    [loginbutton setTitle:@"Login" forState:UIControlStateNormal];


}
return self;
}


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation currentOrientation = [[UIDevice currentDevice]orientation];

switch (currentOrientation)
{
    case UIInterfaceOrientationLandscapeLeft:
    case UIInterfaceOrientationLandscapeRight:

        username.center = CGPointMake(157, 71);
        password.center = CGPointMake(157,144);
        txtusername.center = CGPointMake(146,100);
        txtpassword.center = CGPointMake(120,172);
        loginbutton.center = CGPointMake(214,225);
        break;


    case UIInterfaceOrientationPortrait:
    case UIInterfaceOrientationPortraitUpsideDown:
        username.bounds = CGRectMake(53, 115, 83, 21);
        //username.center = CGPointMake(53, 115);
        password.bounds = CGRectMake(53, 188, 75, 21);
        //password.center = CGPointMake(53,188);
        txtusername.center = CGPointMake(42,144);
        txtpassword.center = CGPointMake(42,217);
        loginbutton.center = CGPointMake(110,269);
        break;

}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

Now what seems to be the problem is that the interface is not getting oriented as it has too, the components are not coming to the coordinates as i have gave them when i rotate the iPhone hardware to left and right they are not coming to those coordinates, When my view loads everything is fine but when i rotate my view i get this bug. Please help me out guys Thank you


By looking at the provided code sample it looks like you just have mixed up the concepts of frame, bounds and center.

Let's look at username. When we create and add the label we get frame = {{53, 115}, {83, 21}}, bounds = {{0, 0}, {83, 21}} and center = {94.5, 125.5}.

So the frame is the absolut position of the label in the super view. It is located at x= 53 and y = 115. The second part of the frame is the size, so we have a width of 83 and a height of 21. The bounds are expressed in label own coordinate system. That is why you normally get the location set to {0, 0}. The center point is the center of frame. which is {53 + 83/2, 115 + 21/2} = {94.5, 125.5}

To use the center point to move the username label back to it's original position you would

username.center = CGPointMake(53 + username.frame.size.width/2, 115 + username.frame.size.height/2); 

You can also just set the frame again

username.frame = CGRectMake(53, 115, 83, 21);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜