【问题标题】:Rearranging subviews from 2 line to 1 according to the device orientation根据设备方向将子视图从 2 行重新排列为 1
【发布时间】:2013-02-12 11:39:21
【问题描述】:

G'day 伙计们,

我最初的目标是使用 iOS6.1 的 iPhone,但也欢迎提示和代码在 iOS5.1 的 iPhone 上完美运行。

我有一个自定义 UITableViewCell,其中包含 7 个不同的标签作为子视图:

-(id) initWithStyle: (UITableViewCellStyle) style reuseIdentifier: (NSString*) reuseIdentifier
{
    self = [super initWithStyle : style reuseIdentifier : reuseIdentifier] ;

    if (self){

//Adding Subviews

[self.contentView addSubView : self.myLabel1] ;
[self.contentView addSubView : self.myLabel2] ;
[self.contentView addSubView : self.myLabel3] ;
[self.contentView addSubView : self.myLabel4] ;
[self.contentView addSubView : self.myLabel5] ;
[self.contentView addSubView : self.myLabel6] ;
[self.contentView addSubView : self.myLabel7] ;
}
return self;
}

当我懒惰地实例化标签时,我认为起始方向是纵向并且标签排列在两条不同的线上:

-(UILabel*) myLabel1
{
    if (!_myLabel1){
        _myLabel1 = [UILabel alloc] initWithFrame: CGRectMake(10,30,40,20);
        _myLabel1.numberOfLines = 1;
        _myLabel1.opaque = YES;
}
return _myLabel1; 
}

-(UILabel*) myLabel2
{
    if (!_myLabel2){
        _myLabel2 = [UILabel alloc] initWithFrame: CGRectMake(50,30,40,20);
        _myLabel2.numberOfLines = 1;
        _myLabel2.opaque = YES;
}
return _myLabel2; 
}

-(UILabel*) myLabel3
{
    if (!_myLabel3){
        _myLabel3 = [UILabel alloc] initWithFrame: CGRectMake(90,30,40,20);
        _myLabel3.numberOfLines = 1;
        _myLabel3.opaque = YES;
}
return _myLabel3; 
}


-(UILabel*) myLabel4
{
    if (!_myLabel4){
        _myLabel4 = [UILabel alloc] initWithFrame: CGRectMake(10,60,40,20);
        _myLabel4.numberOfLines = 1;
        _myLabel4.opaque = YES;
}
return _myLabel4; 
}

-(UILabel*) myLabel5
{
    if (!_myLabel5){
        _myLabel5 = [UILabel alloc] initWithFrame: CGRectMake(50,60,40,20);
        _myLabel5.numberOfLines = 1;
        _myLabel5.opaque = YES;
}
return _myLabel5; 
}


-(UILabel*) myLabel6
{
    if (!_myLabel6){
        _myLabel6 = [UILabel alloc] initWithFrame: CGRectMake(90,60,40,20);
        _myLabel6.numberOfLines = 1;
        _myLabel6.opaque = YES;
}
return _myLabel6; 
}


-(UILabel*) myLabel7
{
    if (!_myLabel7){
        _myLabel7 = [UILabel alloc] initWithFrame: CGRectMake(130,60,40,20);
        _myLabel7.numberOfLines = 1;
        _myLabel7.opaque = YES;
}
return _myLabel7; 
}

我想问一下最好的方法是什么:

“如果方向是纵向,则将标签排列在 2 条不同的行上,如果方向是横向,则将所有标签排列在一行上。如果用户改变方向,重新排列应该是自动的。

横向模式下的重新排列应使所有标签与 y 值 30 对齐(而不是第一行为 30,第二行为 60)。标签 4、5、6、7 的 x 值也应该改变,因为它们不会被放置在标签 1、2、3 的下方,而是被放置在标签 3 的右侧。”

顺便说一句,我发现有时在真正的 iPhone 中启动应用程序时,桌面上会出现“纵向”,在 iOS 5.1 和 iOS 6.1 中,方向都被错误地报告为“横向”。我发现这个是因为我开始使用 layoutSubviews:因为我认为这是重新排列视图的最佳位置,根据设备的方向为每个视图创建一个新框架。这是自动布局的最佳案例吗?该视图已经并将以编程方式创建。

谢谢

尼古拉

【问题讨论】:

    标签: ios uiview autolayout layoutsubviews


    【解决方案1】:

    感谢您的回答 Kalpesh 但实际上经过更多试验后,我发现处理它的最佳方法是在 UITableViewCell 中有一个指向其控制器的指针并使用它来获取新方向,然后在 layoutSubviews 中进行重新排列:

    @property(weak,nonatomic) UITableViewController *controller;
    
    -(void) layoutSubviews
    {
        [super layoutSubviews];
    
    
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    
            // I am on a Phone
    
            NSLog(@"The new controller orientation is: %u", self.controller.interfaceOrientation);
    
            if(self.controller.interfaceOrientation == UIInterfaceOrientationPortrait){
                NSLog(@"Phone - Controller orientation: Portrait.");
    
                //Layout the subviews for portrait orientation
    
                self.myLabel1.frame = CGRectMake(10,70,(self.frame.size.width-20)/4,20);
    
                //..
    
            } else{
                NSLog(@"Phone - Controller orientation: Landscape.");
    
                //Layout the subviews for landscape orientation
    
                self.myLabel1.frame = CGRectMake(10,30,(self.frame.size.width-20)/4,20);
    
                //..
            }
    
        } else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
    
            // I am on a Pad
    
            if(self.controller.interfaceOrientation == UIInterfaceOrientationPortrait){
                NSLog(@"Pad - Controller orientation: Portrait.");
    
                //Layout the subviews for portrait orientation
    
            } else{
                NSLog(@"Pad - Controller orientation: Landscape.");
    
                //Layout the subviews for landscape orientation
            }
    
        }
    }
    

    无论如何,按照您的建议刷新表格对于更改八行很有用!

    编辑:当方向改变时,单元格高度会自动重新加载,因此无需强制重新加载表格。 尼古拉

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多