热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用自动布局实现按钮的网格布局-AchievingGridLayoutofButtonswithAutolayout

IamtryingtoachievealayoutofbuttonsverysimilartoApplesinbuiltCalculatorapp:我正在尝试实现与

I am trying to achieve a layout of buttons very similar to Apple's inbuilt Calculator app: Closeup of calc application

我正在尝试实现与苹果内置的计算器应用非常相似的按钮布局:

I am using autolayout to position the buttons, and drawing a 0.5px border on each button, aiming for a 1px gap between buttons (like the calc app, closeup showing pixels above, from a retina device).

我使用autolayout来定位按钮,并在每个按钮上画一个0.5像素的边框,目标是在按钮之间有一个1px的间隙(像calc app,上面显示的是一个视网膜设备上的像素)。

Border applied by :

边界采用:

    btn.layer.borderWidth=0.5f;
    btn.layer.borderColor=[[UIColor blackColor] CGColor];

and the layout is set up using Interface Builder.

布局是用接口构建器建立的。

app screenshot

It almost works; but there is some variability in the gap between buttons - for example the gap between the "1,2,3,thru" row and the "4,5,6,and" row is 2 pixels, but the gap between the "7,8,9" and "cl,0,@" rows is only one pixel.

它几乎作品;但是在按钮之间的间隔中有一些变化——例如“1、2、3、thru”行和“4、5、6”和“row是2像素的,但是“7、8、9”和“cl、0、@”行之间的间隔只有一个像素。

Constraints are :

约束:

  • Top black area has a fixed height
  • 顶部黑色区域有一个固定的高度
  • All buttons same height
  • 所有按钮一样高
  • In the numeric rows, numeric buttons are set to same width, THRU, AND, ALL OFF and ENTER buttons are set to same fixed width
  • 在数字行中,数值按钮被设置为相同的宽度,通过,和,全部关闭和输入按钮被设置为相同的固定宽度
  • Top row of buttons pinned to bottom of black area, bottom row pinned to bottom of superview In IB the buttons are all positioned butting up against each other.
  • 顶部的一排按钮被固定在黑色区域的底部,底部的一排按钮被固定在ibin的父视图的底部。

In interface builder and at runtime I don't see any autolayout errors; and a debugger dump of the autolayout info gives me:

在接口构建器和运行时,我没有看到任何自动布局错误;一个调试器转储的自动布局信息给了我:

po [[UIWindow keyWindow] _autolayoutTrace]

* - AMBIGUOUS LAYOUT
|   *
|   |   *
|   |   |   *
|   |   |   |   *
|   |   |   |   |   *
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *
|   |   |   |   |   |   
|   |   |   |   |   *<_UILayoutGuide:0x1090e6dd0> - AMBIGUOUS LAYOUT
|   |   |   |   |   *<_UILayoutGuide:0x1090ce080> - AMBIGUOUS LAYOUT
|   |   
|   |   |   <_UINavigationBarBackground:0x109087240>
|   |   |   |   <_UIBackdropView:0x1090899f0>
|   |   |   |   |   <_UIBackdropEffectView:0x10908b110>
|   |   |   |   |   
|   |   |   |   
|   |   |   
|   |   |   |   
|   |   |   
|   |   |   |   
|   |   |   <_UINavigationBarBackIndicatorView:0x109098c80>

Can anyone offer any hints on how to achieve a "pixel perfect" layout like this with Autolayout? Or should I be doing this programatically instead?

有没有人能提供一些关于如何实现像这样的“像素完美”布局的提示?或者我应该按程序来做?

2 个解决方案

#1


1  

I tried to set horizontal/vertical spaces between views equal to 0.5. The color of parent view acts as border color. It looks ugly in IB, it cannot layout views properly but resulting layout on device looks perfect.

我试着在视图之间设置水平/垂直的空间等于0.5。父视图的颜色作为边框颜色。它在IB中看起来很难看,不能正确布局视图,但是设备上的结果布局看起来很完美。

In interface builder:

在界面构建器:

in interface builder

On simulator:

模拟器:

on device

Constraints are a bit tricky. For each row:

约束条件有点棘手。为每一行:

  • set height for the 1st button
  • 设置第一个按钮的高度
  • set equal height for all buttons
  • 设置所有按钮的等高
  • set equal width for all buttons (a bit different for rows with orange buttons)
  • 为所有按钮设置相同的宽度(橙色按钮的行有一点不同)
  • align top edges of all buttons
  • 对齐所有按钮的顶部边缘
  • align bottom edges of all buttons
  • 对齐所有按钮的底部边缘
  • set leading and trailing spaces between all buttons (including spaces to superview) to 0.5
  • 将所有按钮(包括超视图的空格)之间的前导和后置空格设置为0.5
  • set top space (to previous row) of 1st button to 0.5
  • 将第一个按钮的顶部空间(到前一行)设置为0.5

You can apply constraints to multiple items at once. In order to apply leading/trailing spaces between buttons, place them without intersection in desired order before applying.

您可以同时对多个项目应用约束。为了在按钮之间应用前导/尾随空格,在应用之前将它们放置在需要的顺序中。

#2


4  

Layer borders are drawn inside the views bounds, so you aren't going to see a "gap" between the buttons, but the effect of two borders abutting each other. This isn't going to work in the non-retina simulator or devices, since it can't draw a half-pixel, and Autolayout doesn't align views on half-pixels.

层边界是在视图边界内绘制的,所以您不会看到按钮之间的“间隙”,而是两个相邻边界的影响。这在非视网膜模拟器或设备上是行不通的,因为它不能绘制半像素,自动布局也不能在半像素上对齐视图。

For a layout like the one above I'd be giving a fixed height to the buttons (or to one button, and making the others equal it) and letting the black view take up the remaining space. It's always best to have at least one (ideally, exactly one) variable element in a full-screen layout basically to cover rounding errors.

对于上面这样的布局,我将给按钮(或一个按钮,让其他按钮相等)一个固定的高度,让黑色视图占据剩余的空间。最好在全屏布局中至少有一个(理想情况下是一个)变量元素,以基本覆盖舍入错误。

It sounds at the moment like you have one fixed element (the black view) and the buttons are basically "take the remaining height and divide it between yourselves", which will only work if the remaining height divides perfectly.

这听起来就像你有一个固定的元素(黑色视图),按钮基本上是“取剩下的高度,然后把它分成两部分”,只有当剩下的高度完全分割的时候才会起作用。


推荐阅读
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • VueCLI多页分目录打包的步骤记录
    本文介绍了使用VueCLI进行多页分目录打包的步骤,包括页面目录结构、安装依赖、获取Vue CLI需要的多页对象等内容。同时还提供了自定义不同模块页面标题的方法。 ... [详细]
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
author-avatar
zhu宝宝meng
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有