如何在视图中将标签位置从纵向设置为横向

时间:2011-10-18 08:51:31

标签: iphone objective-c xcode

我正在做申请,我已经拍摄了两个视图肖像和风景。我正在创建一个标签,并通过参数传递来调用它。因为我希望在landscapeView中更改标签的位置,所以我正在拍摄两个视图。相反,我想在SingleView中进行操作

而不是使用两个视图我想只有一个视图,并在纵向和横向上相应地设置标签位置。

所以请建议我如何只有一个视图并根据视图更改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;     
}

4 个答案:

答案 0 :(得分:0)

在您的代码中有一个名为

的方法
     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandScape)
//set label frame here
}

答案 1 :(得分:0)

在viewcontroller中实现以下两个方法。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

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

willRotateToInterfaceOrientation:duration:方法中,将所需的帧设置为标签实例。

答案 2 :(得分:0)

  1. 通过在.h文件中声明它,使您的UILabel * myLabel成为一个类变量。
  2. 根据正确的设备方向,在viewController的viewDidLoad方法中调用createLabel方法。
  3. - (void)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. 使用此方法重置框架
    2. - (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:";
             }
      }
      

      你需要挖掘一下,以涵盖这种方法中所有可能的方向。

      干杯!!!

答案 3 :(得分:0)

您应该在layoutSubviews超级视图的UILabel方法中设置标签框,并检查超视图边界或界面方向以确定标签的相应框架。如果您还正确使用autoresizingMask上的UILabel,那么过渡效果会非常好。

另外:你为什么使用两种观点?而不是将不同的布局应用于一个?