提高Interface Builder高效工作的8个技巧

源地址:http://codesheriff.blogspot.hk/2014/03/8-tips-for-working-effectively-with.html


8 Tips for working effectively with Interface Builder

Introduction
While working at JoyTunes on the latest versions of our top-notch piano app, we've done a great deal of UI redesign and therefore had to spend long hours with the notorious Interface Builder, resizing images and views, which can be a lot of frustrating work.
During this work, we actually discovered a great deal of neat IB tricks, and I decided I must share them with the world.
So I conducted this little post of my 8 greatest tips for working effectively with IB.

A little disclaimer:
In JoyTunes, we work with .xib files (not storyboards), and without Auto Layout. This is mostly for historical reasons. As a result, some of the tips I'm mentioning might react a little differently for you if you're using storyboards or Auto Layout, but most should apply just the same.

1. Size to Fit Content
I'm so ashamed I only took the effort to look this up only recently. A HUGE time saver.
Select any view and then go to Editor->Size to Fit Content or simply press ⌘=
Will resize the selected view to fit its content in the following manner:

  • An ImageView/Button will be resized to the image's original size (most common usage):



  • A Label/Button will be resized to fit the text it's currently containing:


  • A parent container view to fit the frames of its subviews.


2. Holding option key to view distance from another view's edges
Holding option key after selecting a view, and then hovering over some other view, will show you cool distance indicators from that view's edges: 







3. Editor -> Embed In View, Unembed:
Do you know that annoying feeling when you have some subviews all located and then you decide they need to have a different parent view, or even a different .xib, and then they are all automatically get located in the center of the new view?
Well - here's the solution:




4. Adding padding to freeform sized view without affecting subview's positions
When trying to add padding to a view, the x's and y's of the subview is retained, which isn't always what you want.
The best way I found to get past this, is to drag the edges while holding ⌘:




5. Move a view that isn't in the front:
One thing that bugs me when working with IB, is the inability to move views that aren't in front easily.
One way to do so will be to temporary bring it to front and then bring it back, but this is annoying.
Another way will be to use the size inspector on the right panel, which is fine but could take some guessing and adjustments until you get it right.

A way I found of moving a view with the arrow keys without bringing it to front is this:
1. Choose the view on the document outline.
2. Click on the frame of the root view in order to gain focus.
3. Move with arrow keys.
EDIT:
Some other (better) way of gaining focus, taken from the comments to this post:

  • If you double-click a view in the outline, it will switch focus to the layout pane with that view selected.
  • Shift+Right(Ctrl) Click over the view in IB and you'll see a menu of all the views that exist where you've clicked.

6. IBOutletCollection ordering
Often, the order of IBOutletColletions is important to us, and we want to iterate on them in code in that order.
One way of doing it is sorting them in code by their x/y/tag, and then iterate.
This is unnecessary. The order of connection is determined by the order in which we dragged the connection, and we can see the current order if by ⌃+Clicking the File's Owner:


7. Use custom properties 
I think a relatively underused feature of IB is the ability to set custom properties on views, using the "User Defined Runtime Attributes" of the Identity inspector:

As you can probably guess, we defined a JTLabel class that you can set stroke color and width to, and this is our way of immediately setting this without having to write any code.
Another common hack that can be applied this way is setting "layer.cornerRadius" of any view, to make it have rounded corners.

A relevant tool that uses this mechanism is Canvas, which will let you define animations for views without writing code.

Important warning regarding this that saved my life on one occasion:
In your implementation class you cannot use initWithCoder: otherwise your key-value path setting will not be picked up. You will have to do all your implementation within awakeFromNib.

8. MoarFonts - custom fonts WYSIWYG
Custom fonts have always been a big issue when working with Interface Builder.
There's no built-in support for those, and the best solutions I have seen were either using custom properties solutions like Canvas, or using font replacing techniques like IBCustomFonts. These are valid solutions, but they have a great downside - they don't give you a full WYSIWYG experience, which is exactly why you are using Interface Builder in the first place.
It can be very annoying to place a label with a placeholder font and then having to run the app in order to discover that the width you gave it doesn't fit well with your replacing custom font.

Lately I stumbled upon this solution called MoarFonts. It costs 10$, and there's no demo or trial - but trust me, it's totally worth it!
It's a pretty simple setup: you'll have to build your app with MoarFonts as a script build phase, restart Xcode, and voila! You can see custom fonts in InterfaceBuilder:



先来看看目录:
介绍
使view的Size与view中的Content相适应
按住option键—观察所选中view与另外view边缘之间的距离
Editor –> Embed In View, Unembed:
在不影响subview的位置时给view自由的添加padding
对不在最前端的view进行移动
IBOutletCollection排序
使用自定义属性
MoarFonts——字体定制:所见即所得
 
介绍
在JoyTunes工作期间,我们在开发最新一版的钢琴应用程序,对程序的UI做了大量的重新设计,因而也在Interface Builder上花费了许多时间,对于图片和view的缩放操作,非常的让人不省心。不过在开发过程中,我们发现了许多非常不错的IB使用技巧,我寻思着这必须要跟大家分享,所以成就了这篇文章。
 
免责声明: 在JoyTune工作期间,我们使用的是.xib文件(不是storyboards),并且没有使用Auto Layout。实际上这主要是历史原因导致的。所以,这里介绍的一些技巧可能稍微有点不同(如果你使用storyboard或Auto Layout),不过大多数都是一样的。
 
1. 使view的Size与view中的Content相适应
很惭愧的是最近才发现这个功能——能节约大量时间。 选中任意的一个view,然后Editor->Size to Fit Content,或者简单的按 ?= 接着就会按照下面的规则对选中view的Size做出与之Content对应的适应。
ImageView/Button的size会设置为图像的原始size(最常见的用法):

Label/Button的size会被设置为与当前text内容相当的尺寸:

parent container view会与其subviews的frames相适应。

2. 按住option键—观察所选中view与另外view边缘之间的距离
按住option键之后,选择一个view,然后将鼠标悬停在别的一些view上,会看到一些距离——选中view与别的view边缘之间的距离。

3. Editor –> Embed In View, Unembed:
你是不是对此素手无策呢:你希望将已有的一些subviews放入到不同的parent view中,甚至是不同的.xib文件中,但是当你把一些view重新设置之后,它们为自动的位于新的parent view中心?
 
现在好了,我们有一个解决办法,如下图所示:

4. 在不影响subview的位置时给view自由的添加padding
当试图给view添加padding时,默认情况下subview的x和y是不会改变的,但是有时候我们并不希望是这样的结果。我发现一个最好的方法,就是在按住?时拖动view的边缘:

5. 对不在最前端的view进行移动
刚开始我还以为要想移动不在最前端的view是不可行的。
 
有一种方法就是先将非最前端的view临时设置到最前端,移动好位置之后,在设置回去。
 
另外一种方法就是使用右边panel中的size inspector,不过有时候要想设置一个好的位置,需要不断的猜测和修正。
 
另外我发现一种方法:使用键盘上的上下左右键来移动view——这还不用把view设置为最前端:
 
1.在document outline中选中view
2.为了获得view的焦点:单击root view的frame
3.利用箭头进行移动

提醒: 获得view的焦点还有一个更好的方法:在document outline上双击view,就可以用箭头移动view了。
 
6. IBOutletCollection排序
有时候IBOutletCollection里面元素的顺序对我们来说非常重要:我们希望按某个顺序对其进行迭代。
 
有一种方法:在代码里面利用x/y/tag对其做排序处理,然后在迭代。
 
实际上,没必要这么做。IBOutletCollection的顺序取决于我们dragged connection的顺序,可以通过^+单击 File’s Owner来查看当前的顺序:

7. 使用自定义属性
可能这个功能是IB中很少被使用的:使用Identity inspector中的User Defined Runtime Attributes(用户自定义运行时属性)在view上设置自定义属性:

在此我定义了一个JTLabel类,我们可以设置它的stroke color和width,这样一来我们就不用在代码里面设置相关属性了。
 
利用这个功能很好的一例子就是Canvas,通过它不用写一行代码就能定义相关的动画。
 
8. MoarFonts——字体定制:所见即所得
在Interface Builder中字体的定制是个非常麻烦的事情。IB并没有内置该功能,我用过比较好的解决办法就是使用自定义属性——就像Canvas一样,或者使用字体替换技术——例如 IBCustomFonts。这些都是有效的方案,不过他们有一个致命的缺点——它们不能给我们一种WYSIWYG(所见即所得)的体验,当然,这也是为什么我们会第一时间使用Interface Builder的原因。
 
为了知道给label设置的自定义字体是否合适,我们必须要运行程序才能知道结果——这有点让人不能接受。
 
最近我发现了一个新的解决办法:使用 MoarFonts。卖价10美元,没有demo,没有试用——不过请相信,这非常值得购买!它的使用方法非常简单:将MoarFonts当做script build phase,然后build app,接着重启Xcode,就可在Interface Builder中看到定制的字体。
 
打完收工!希望这些技巧对你能有所帮助。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值