iphone定向不工作
嗨,我有一个像这样的代码iphone定向不工作,iphone,objective-c,Iphone,Objective C,嗨,我有一个像这样的代码 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization username = [[UILabel alloc]initWithFrame:CGRect
- (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;
}
现在的问题似乎是界面没有像现在这样定向,组件没有达到我给它们的坐标,当我左右旋转iPhone硬件时,它们没有达到这些坐标,当我的视图加载时,一切正常,但当我旋转视图时,我得到了这个错误。请帮帮我,伙计们
感谢您通过查看提供的代码示例,您似乎混淆了框架、边界和中心的概念 让我们看看用户名。当我们创建并添加标签时,我们得到frame={53115}、{83,21}、bounds={0,0}、{83,21}和center={94.5125.5} 因此,框架是标签在超级视图中的绝对位置。它位于x=53和y=115处。框架的第二部分是尺寸,所以我们的宽度是83,高度是21。边界以标签自身的坐标系表示。这就是为什么通常将位置设置为{0,0}。中心点是框架的中心。也就是{53+83/2,115+21/2}={94.5125.5} 要使用中心点将用户名标签移回其原始位置,您需要
username.center = CGPointMake(53 + username.frame.size.width/2, 115 + username.frame.size.height/2);
也可以重新设置帧
username.frame = CGRectMake(53, 115, 83, 21);
您是否尝试过检查从currentOrientation获得的价值?我以前在[[UIDevice currentDevice]方向]上遇到过错误,我发现最好使用[self interfaceOrientation],假设self是UIViewController(或子类)。我已经替换了行IIinterfaceOrientation currentOrientation=[[UIDevice currentDevice]方向];使用UIInterfaceOrientation currentOrientation=[self interfaceOrientation];但是没用,还是没能做到…设置框架成功了谢谢Robert澄清了我的概念,