iOS两个视图恰好覆盖了父视图的一半

问题描述 投票:2回答:6

在我的应用程序中,我想实现此布局:

因此父视图包含两个子视图。第一个恰好在中间(高度/ 2)结束,第二个在父视图的中间开始。我发现在受限制的IB中不可能做到这一点。所以我在viewDidLoad方法中使用了这段代码:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:firstView
                                                              attribute:NSLayoutAttributeHeight
                                                              relatedBy:0
                                                                 toItem:self.view
                                                              attribute:NSLayoutAttributeHeight
                                                             multiplier:0.5
                                                               constant:0];

[self.view addConstraint:constraint];

现在它可以工作,但只有当应用程序在iPhone上运行时。因为视图的大小就像iPhone屏幕。如果此应用程序在iPad上运行,则会出现问题,因为屏幕大小不同,因此此父视图更长。并且约束(上面的代码)仍然需要从IB的视图大小的0.5 *大小,而不是从视图的iPad大小的大小。物品toItem:self.view仍然需要IB的规模。

结果是该视图在iPad中的大小与iPhone中的大小相同。在iPad上有一个很大的空白区域,然后有一个iPhone大小的视图。

你能说出我必须做些什么才能使它适用于各种屏幕尺寸?非常感谢你

ios layout view constraints
6个回答
5
投票

这可能是使用约束,但是由IB相当烦人且不灵活的约束管理器使它变得有点繁琐。以下是我管理它的方式:

  • 在IB中,使用正确的帧设置两个视图
  • 在两个视图之间添加相等的高度约束
  • 降低任一视图上任何默认高度约束的优先级。不幸的是,IB不允许你完全删除它们,但是将它们设置为小于1000的任何值将确保它们被忽略。
  • 在视图控制器viewDidLoad方法中,添加您已尝试过的约束。

例如

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.topView
                                                                  attribute:NSLayoutAttributeHeight
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:self.view
                                                                  attribute:NSLayoutAttributeHeight
                                                                 multiplier:0.5
                                                                   constant:0];
    [self.view addConstraint:constraint];
}

而已。 IB约束的屏幕抓图如下所示:


2
投票

试试这个代码。它将动态设置约束值

在.h文件中,实现这一行。

  #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  #define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *TopsubviewheightConstraint;

现在按照给定的屏幕截图创建此约束

从屏幕连接TopsubviewheightConstraint高度约束

在.m文件中实现此代码

if (IS_IPHONE_5) 
    _TopSuperViewConstraint.constant = 275;
else if(IS_IPAD)
    _TopSuperViewConstraint.constant = 502;
else
    _TopSuperViewConstraint.constant = 230;

我希望它会对你有所帮助。


1
投票

你有2个选择。

  1. 为iPad创建第二个IB文件
  2. 通过程序做所有事情并使用[[UIScreen mainScreen] bound];而不是获得父母的大小;)

我会在没有约束的情况下完成它并设置如下:

// self.view is my container view

CGRect frame = [[UIScreen mainScreen] bound];
frame.size.height /= 2;    

// upper View
upperView.frame = frame;

// lower View
frame.origin.y = frame.size.height;
// or alternatively
//frame.origin.y = CGRectGetMaxY(frame);
lowerView.frame = frame;

在这里,您不需要任何特定于设备的选项,一切都是动态的,绑定到设备屏幕的大小;)


1
投票

好的,所以我想出了如何做到这一点。只需将代码放入viewDidLayoutSubviews方法而不是viewDidLoad。我在Unable to set frame correctly before viewDidAppear主题中找到的解决方案。

这是我的代码:

[subView1 setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)];
[subView2 setFrame:CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2)];

感谢所有的努力!


1
投票

感谢Tark的回答,我也设法使用约束:

  1. 将TobView的垂直空间约束添加到Top Layout Guide(使用StoryBoard)
  2. 将BottomView的垂直空间约束添加到Bottom Layout Guide(使用StoryBoard)
  3. 为ViewDidLoad中的每个视图添加两个高度约束

码:

NSLayoutConstraint *constraint;
constraint = [NSLayoutConstraint constraintWithItem:_viewTop
                                          attribute:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:self.view
                                          attribute:NSLayoutAttributeHeight
                                         multiplier:0.5
                                           constant:0];


[self.view addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:_viewBottom
                                          attribute:NSLayoutAttributeHeight
                                          relatedBy:NSLayoutRelationEqual
                                             toItem:self.view
                                          attribute:NSLayoutAttributeHeight
                                         multiplier:0.5
                                           constant:0];
[self.view addConstraint:constraint];

0
投票

You can do this with constrains (no code required!)

1.-首先创建两个UIview并手动将其高度设置为当前设备大小的一半,将一个定位在另一个上,如下所示:

2.-接下来你必须为它们中的每一个设置约束(这将允许容器填满整个屏幕,一个覆盖另一个):

顶部容器

底部容器

3.-最后,您必须选择两个容器并添加一个新约束,指定它们将具有相同的高度

(记得为每一步点击“添加X约束”)

现在它应该准备好将标签放在每个容器内,你就可以了

© www.soinside.com 2019 - 2024. All rights reserved.