在开发中我们经常会遇到下面图片这种需求

左边一个UIImageView 右边两个UILable,然后这两个lable分别底部和顶部对应imageView的中间,如果用Masonry代码写就比较简答,那如果用xib呢,之前一直回避这个问题,全是用代码写的,今天就研究了下用xib如何实现。
首先建立一个工程,

在Main.storyboard里面拖入两个UILable和一个UIImageVIew,和一个UIView,这个UIView是当一个容器来放置label的,这个思路来源于里脊串的一篇博客,具体是这个uiview的中部对应imageview的中部,uiview的top,bottom,left,right对应lable。废话不多说,一步步实现。
1,选中uiimageview,给uiimageview加约束,宽高100,距离左边10,上边10.

2,把两个uilab拖入空视图中,给第一个lable加约束,距离空白视图顶部0,lef 为0;

3,选中第2个lable,添加约束,距离空白 view left 为0,顶部距离第一个lable底部为0;

4,选中空白uiview按着Ctr键拖动到第2个lable,选择 Bottom Space to Container

5,选中空白uiview按着Ctr键拖动到第1个lable(其实第2个也行,到时间就看你那个lable长,依据就是哪一个),选择Trailing Space to Container

6,选中空白uiview按着Ctr键拖动到imageview 选择Center Vertically

7,选中空白uiview,设置其跟uiimageview的间距是10

8,然后更新视图 大概会看到这样的视图,跟我们需要的视图还不太一样,接下来修改,

9,选中空白uiview,然后点击右边这个选项,会看到一系列的约束。

其中有两个选项是这样的带有数字,分别表示,uilable的右边到空白view的右边距离是70,和uilable的底部到空白view的底部距离是54,把这两个值修改为0;

10,接下来如果不出意外的话,视图就是我们想要的效果了。

其实一堆约束拖下来,感觉还没有写起来的约束快,但是有些页面,使用xib拖的话,要不手写的代码快多了,如果你看了觉得我描述不清楚的话,我上传了一个demo,地址在这里https://github.com/yunisSong/YunisTool
里面还包含了两个小工具。
des加解密,
获取地理位置及地理位置坐标
网友评论