2011-10-18 60 views
0

我正在做应用程序,其中我采取了两个视图纵向和横向。我正在创建一个标签,并通过参数传递来调用它。因为我希望在landscapeView中更改标签的位置,所以我采用了两个视图。相反,我想在单视图中执行此操作,而不是使用两个视图意见我想只有一个视图,并在纵向和横向相应地设置标签的位置。如何在视图中设置从纵向到横向的标签位置

所以请建议我如何只有一个视图,并根据视图更改textLabel位置。请向我推荐示例代码。

目前我使用下面的代码

[self.portraitView addSubview:[self createLabel:CGRectMake(450,140,60,20):@"Ratings:"]];  
[self.landscapeView addSubview:[self createLabel:CGRectMake(600,140,60,20):@"Ratings:"]]; 

-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle { 
    UILabel *myLabel = [[UILabel alloc] initWithFrame:frame]; 
    [UIFont fontWithName:@"Arial" size:13]; 
    myLabel.text = labelTitle;  
} 

回答

0
  1. 通过在你的.h文件中声明它,使你的UILabel * myLabel成为一个类变量。
  2. 根据正确的设备方向,在viewController的viewDidLoad方法中调用createLabel方法。

- (无效)viewDidLoad中

{ 
     UIDeviceOrientation orientation = [UIDevice currentDevice].orientation 

if(orientation == UIDeviceOrientationPortrait) 
      CGRect frame = CGRectMake(450,140,60,20); 
else 
      CGRect frame = CGRectMake(600,140,60,20); 

     myLabel = [[UILabel alloc] initWithFrame:frame]; 
     [UIFont fontWithName:@"Arial" size:13]; 
     myLabel.text = labelTitle;  
} 
  1. 复位帧在该方法中

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{ 
    if(interfaceOrientation == UIInterfaceOrientationLandScape) 
      {  
       mylabel.frame = CGRectMake(600,140,60,20); //your landscape frame 
       mylabel.text = @"Ratings:"; 
      } 
    else 
     { 
      mylabel.frame = CGRectMake(450,140,60,20); // your portrait frame 
      mylabel.text = @"Ratings:"; 
     } 
} 

您将需要为了涵盖这种方法中所有可能的方向,挖一点点。

干杯!

+0

如果声明Wat代码我应该添加?给我示例代码 – crazy2431

+0

如果问题仍然存在,请接受解决问题或更新评论的答案。 – SayeedHussain

+0

谢谢你的回应...但它显示未使用的变量框架和LableTitle未声明...而且我使用参数传递在我的代码中,因为..我需要在我的视图中创建许多标签文本..所以我使用参数传递,因为我有许多其他文本要手动创建.. – crazy2431

0

在代码中有一个名为

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
if(interfaceOrientation == UIInterfaceOrientationLandScape) 
//set label frame here 
} 
+0

[self.view addSubview:[self createLabel:CGRectMake(650,170,60,20):@“Reviews:”]];我应该把上面的代码放在这里吗?还有它给出警告以返回一个值......上面的代码是正确的还是shd我只是设置了框架..? – crazy2431

0

在你的ViewController以下两种方法实现方法。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

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

willRotateToInterfaceOrientation:duration:方法,设置所需帧到标签实例。

+0

好吧我在哪里添加下面的代码[自我。查看addSubview:[self createLabel:CGRectMake(650,140,​​60,20):@“Ratings:”]]; – crazy2431

+0

请为UILabel创建一个班级成员。分配该实例并将其作为子视图添加到主视图。旋转时,只需将框架设置为UILabel实例。 – Ilanchezhian

+0

其实我不想创建一个单一的文本。在我的应用程序中,我将在不同的框架位置创建10个标签文本..因此,我使用上述代码中的参数传递..但为什么需要为UILabel创建一个类成员? – crazy2431

0

您应该在UILabel的超级视图的layoutSubviews方法中设置标签框,并检查超级视图边界或界面方向以确定标签的相应框架。如果您还正确使用UILabel上的autoresizingMask,则转换将会很好地生成动画。

另外:为什么你使用两个视图?而不是对一个应用不同的布局?

+0

我可以有示例代码,因为我是新来的我是新的iphone开发 – crazy2431

相关问题