简体   繁体   English

宽度/高度按钮相等的网格上的iOS约束导致位置和大小变化

[英]iOS Constraints on Grid of Equal Width/Height Buttons cause positioning and size to vary

I'm updating my Calculator app to use new dynamically sized buttons and having a problem with the constraints. 我正在更新我的计算器应用程序,以使用新的动态尺寸的按钮,并且约束方面存在问题。 The contains should work fine as far as I can tell, but Xcode complains about misplaced views. 据我所知,该容器应该可以正常工作,但是Xcode抱怨视图放置错误。 The values it gives for the misplaced views are not proper and if I try to fix them, then it just causes more views to be "misplaced." 它为错位的视图提供的值不正确,如果我尝试对其进行修复,则只会导致更多的视图“错位”。

In the screenshot below, the first multi-colored row (orange, green, pink, yellow, red) is a row of UILabels. 在下面的屏幕截图中,第一个彩色行(橙色,绿色,粉红色,黄色,红色)是UILabels行。 They should all have equal widths with 0 spacing between them and the edges of the screen. 它们和屏幕边缘之间的宽度都应相等,且间隔为0。 They are in a container view (Xcode label is "Register Label View", shown in a screenshot below that one.) The Register Label View container has a fixed height constraint. 它们在容器视图中(Xcode标签是“ Register Label View”,如下面的屏幕快照所示。)Register Label View容器具有固定的高度限制。

Then below that, is another container view ("Keypad View") and inside that is a 5x8 grid of views (the buttons on my calculator). 然后在其下是另一个容器视图(“键盘视图”),内部是5x8的视图网格(计算器上的按钮)。 The buttons should all have the same widths and all have the same heights. 按钮的宽度和宽度都应相同。 In my storyboard, they are 64x58 which fits perfectly within my 320x464 Keypad View. 在我的情节提要中,它们的尺寸为64x58,非常适合我的320x464键盘视图。 (320/5 = 64, and 464/8=58). (320/5 = 64,和464/8 = 58)。

在此处输入图片说明

Below is a screenshot of the Storyboard warnings. 以下是情节提要警告的屏幕截图。 Notice the last two items are Dynamic Buttons (in the Keypad View) and it says that one of them is expected to be 63 wide and the other is expected to be 65 wide. 请注意,最后两个项目是“动态按钮”(在“键盘视图”中),它说其中一个预期为63宽,另一个预期为65宽。 That's not correct. 那是不对的。 They are all supposed to be 64 wide and they all have an Equal Widths constraint. 它们都应该是64宽,并且都具有等宽约束。

One of the labels also seems to have the same problem where Xcode expects one of them to be 63 wide, but they should all be 64 wide. 其中一个标签似乎也有相同的问题,Xcode希望其中一个为63宽,但它们都应为64宽。

在此处输入图片说明

You can see in the Storyboard that the buttons all line up nicely without overlapping (based on their frames), but when I run it in the Simulator, you can see them overlapping each other by one pixel in different directions on each row: 您可以在情节提要中看到按钮的所有排列都很好,没有重叠(基于它们的框架),但是当我在Simulator中运行它时,您可以看到它们在每一行的不同方向上相互重叠一个像素:

在此处输入图片说明

The way I set up the constraints: I arranged them all using their frame (x,y,w,h) in the storyboard, then I selected them all, and added constraints like this: 设置约束的方式:在情节提要中使用它们的框架(x,y,w,h)排列了所有约束,然后选择了所有约束,并添加了如下约束:

在此处输入图片说明

在此处输入图片说明

But then it immediately complains that several of the items are misplaced. 但随后它立即抱怨其中一些物品放错了位置。 What am I doing wrong? 我究竟做错了什么? How can I make this work properly? 我该如何使其正常工作?

Ok, I figured out how to make it work with only constraints. 好的,我弄清楚了如何使其仅受约束。 There were just too many interconnected dependencies and the system couldn't handle it. 互连依赖项太多了,系统无法处理它。 I was selecting the entire grid of buttons and adding Equal Heights and Equal Widths constraints to all of them, which meant each button was dependent upon every other button. 我选择了整个按钮网格,并向所有按钮添加了“相等高度”和“相等宽度”约束,这意味着每个按钮都依赖于每个其他按钮。

Instead, I did it row by row. 相反,我是逐行进行的。 I selected one row: 我选择了一行:

在此处输入图片说明

I applied the Equal Heights and Equal Widths constraints to each item in that row. 我将“相等高度”和“相等宽度”约束应用于该行中的每个项目。 Then I did the same to Row 2, then Row 3, etc. So, each row was depending only on itself. 然后,我对第2行,第3行等进行了同样的操作。因此,每一行仅取决于自身。 However, those constraints weren't enough to define the UI completely, because the Height was still undefined. 但是,这些限制还不足以完全定义UI,因为Height仍未定义。

So, then I selected the first column (ie first item in each row) and applied Equal Widths and Equal Heights constraints to those items. 因此,然后我选择了第一列(即每行中的第一项),并对这些项目应用了“等宽”和“等高”约束。 Then all of the constraints behaved properly and I didn't receive any Misplaced Views warnings. 然后,所有约束行为均正常,并且我未收到任何“放错视图”警告。

I believe this worked because the constraints on the columns could resolve the Height of each row and then row could resolve it's own Widths without having to interact with any other row. 我相信这是可行的,因为列上的约束可以解析每行的高度,然后行可以解析自己的宽度,而不必与其他任何行进行交互。

The small amount of code below (and setting the cell size and minimum spacings in the storyboard) will give you your keyboard using a collection view. 下面的少量代码(并在情节提要中设置单元格大小和最小间距)将使您的键盘使用集合视图。

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@end

@implementation ViewController

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 40;
}

-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = (indexPath.row % 2 == 0)? [UIColor colorWithRed:180/255.0 green:210/255.0 blue:254/255.0 alpha:1] : [UIColor colorWithRed:50/255.0 green:167/255.0 blue:85/255.0 alpha:1];
    return cell;
}

This gives the following result, 得到以下结果,

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM