TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Laying Out iOS UIs in Code

142 点作者 lyinsteve大约 11 年前

28 条评论

bridger大约 11 年前
I would still recommend using Auto Layout, even if you are laying out your views without Interface Builder. (Full disclosure, I helped write Auto Layout.)<p>One advantage is that it makes sure things are pixel-integral, no matter the scale factor of the screen. For example, his code example,<p>button.x = self.view.width * 0.2; &#x2F;&#x2F; Position the inset at 20% of the width.<p>has a pretty good chance at starting the button at x=21.845, or some other point in-between pixels and producing a blurry line. If you do this same relation in Auto Layout, the engine makes sure that the positions and widths are all pixel-integral.<p>This is not as simple as just rounding everything, either! For example, if you have two views<p>[blue][red], you want to make sure you round their shared edge either to the left or to the right. Otherwise, there will be a gap between them. You need to make sure you round their shared edge the same way consistently too, or it will jump back and forth as you resize a window and produce a noticeable jitter.<p>Also, in that same code example, they set a button&#x27;s x position to be directly related to the width of a view. If you ever want to support RTL interfaces, this is a bad idea. It is relating a width to a position. In a RTL interface, the correct code would be<p>button.x = self.view.width - self.view.width * 0.2 - button.width<p>Complicated! In general, you shouldn&#x27;t convert between positions and sizes. Instead, I would make an invisible spacer view and lay them out like this using the layout format language<p>|[spacerView][button]<p>Then make a relation setting the spacerView&#x27;s width to be 0.2 of the superview&#x27;s width. This will produce constraints that correctly work in a RTL interface, laying it out like [button][spacerView]|.
评论 #7331448 未加载
评论 #7331019 未加载
评论 #7331114 未加载
RyanZAG大约 11 年前
There&#x27;s generally two kinds of iOS apps&#x2F;devs and once you work out what bracket you fall into, choosing between IB and code is easy.<p>If you&#x27;re making fairly standard apps that substitute largely for websites - eg, login screens, some data in lists, couple forms - then you will probably want to go with IB storyboards. For these types of apps, development speed is most essential and future changes are mostly just tweaking the UI a bit as a large part of the functionality of the app is just pulling data from web services or doing standard calculations and displaying the result. Storyboards will let you get a nice looking and fairly simple UI done very fast and allow for rapid UI changes.<p>However, if you are going to have 3+ devs working on your app because it&#x27;s actually the basis for a business or is very complicated, storyboards cause a lot of merge problems. They&#x27;re far harder to create automated test code for. Refactoring your app becomes an exercise in tracking down IBOutlets. The couple days you saved at the start with easy layout and transitions get eclipsed by the amount of time you spend fighting IB later for changes. Also, if you use code review tools and have a heavy peer review culture then storyboards are a particularly bad fit.<p>Honestly I believe most apps fit into the first option and storyboards are the way to go. 95%+ of the apps on the Apple Store are definitely in the first category, and there usually isn&#x27;t a need to over engineer them.
评论 #7331040 未加载
评论 #7331209 未加载
评论 #7330968 未加载
评论 #7331367 未加载
评论 #7331896 未加载
agildehaus大约 11 年前
Why not just use Auto Layout in pure code?<p>buttonView = [[UIButton alloc] init];<p>buttonView.translatesAutoresizingMaskIntoConstraints = NO;<p>[self.view addSubview:buttonView];<p>[self.view addConstraint:[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];<p>... etc<p>It&#x27;s certainly better than the stress you&#x27;ll have with UIView+Positioning or IB.
评论 #7331228 未加载
评论 #7332567 未加载
accatyyc大约 11 年前
I have some problems with this post.<p>#1 is saying that you&#x27;re restricted to point based UI&#x27;s in interface builder. This is absolutely not true. Wether you do you UI&#x27;s in interface builder or code - use auto layout!<p>This way you get dynamic, resolution-independent UI&#x27;s that can even work automatically with right-to-left text (realigning other elements according to text alignment).<p>The way the author is doing it is still point-based (just a bit more dynamic since he does some calculations) while auto layout has all this built in.<p>Yes, it is harder to get started with auto layout, but once you learn it, it is worth it.<p>#2, using things like UIView+Positioning still sets your frames. And it does so for each property you set. This means that the frame might be set 5 times instead of one (just for slightly cleaner code). This is not good since it might cause 5 calls to layoutSubviews instead of one (performance issue) and also it will ruin animations (dependent on a source and target frame).
评论 #7330933 未加载
评论 #7330913 未加载
SimianLogic2大约 11 年前
If all of your layouts are created in code, every visual tweak requires an engineer. In general I will do almost anything to NOT have to lay out views by hand, as nothing slows a project down more than jumping into the tweak-a-magic-number-and-recompile cycle. It sucked when I was doing CSS, and it sucks even more when I&#x27;m compiling code.<p>For the most part IB mostly doesn&#x27;t make sense for games, so I have a custom Photoshop script that exports each layer with metadata. A custom importer reads the metadata file and loads the whole view in the proper positioning. So I&#x27;ve basically swapped IB for Photoshop, but the work flow is essentially the same....
评论 #7331670 未加载
vanwesson大约 11 年前
&gt; I use UIView+Positioning...<p>Looking at that code reveals that it doesn&#x27;t try to account for fractional positioning. If you set self.center on a view that is an even number of pixels wide, it will result in a non-pixel-aligned origin on non-retina displays, which results in in blurry rendering. If you&#x27;re iOS-7 and iPhone only these days, that may not matter, but there are still millions of non-retina iPads out there, and in fact you can still buy new ones on the Apple store (both the original iPad mini and the iPad 2 are still for sale).<p>I&#x27;ve written lots of code for iOS and have generally always preferred programmatic layout over IB, but you have to be a little more careful than this post is implying. Beefing up your helper routines to take into account issues like the above is critical to making sure your UIs always look their best.
评论 #7331138 未加载
评论 #7331133 未加载
fomojola大约 11 年前
As someone who has done a decent amount of Swing&#x2F;Android development, I&#x27;ve often wondered: why hasn&#x27;t iOS historically had decent layout manager support? I&#x27;ve used RelativeLayout for Android and quite frankly I&#x27;ve found very few instances where it didn&#x27;t do what I wanted: the reduced number of iOS form factors makes it easier to do pixel-perfect layouts (than the thousands of Android devices) but even AutoLayout seems like a poor replica of what the Swing&#x2F;Android layout manager-style libraries can do. I use it both from the Android XML and from code and it makes life REALLY easy. I&#x27;ve done a lot of work with storyboard&#x2F;IB recently and it isn&#x27;t as bad as I used to think, but always wondered about that.
评论 #7332572 未加载
supercoder大约 11 年前
I definitely side with the author. Any time we&#x27;ve used IB it&#x27;s always ended up in regret. Refactoring, odd layout issues, iPad &#x2F; iPhone management has always made it a headache.<p>It&#x27;s probably a testate to how good UIKit is to code with directly that makes it attractive.<p>Though whatever side you fall on, I think it&#x27;s worth appreciating you have the <i>choice</i>. I remember when doing some brief Windows Phone development, and seemed all you had to work with was some XML API to design your interfaces. It was awful.
mp3jeep01大约 11 年前
One of the bigger takeaways from this article should be to make a decision of where to set layout parameters, and stick with it. I&#x27;m sure we all have opinions on using the in-code method vs IB, and can debate &#x27;till the end of time. As a relatively new iOS dev picking up an existing codebase, the biggest thing I could say is be consistent. The number of times I tried setting something in code only to find it was being changed in IB is far too many.
评论 #7331076 未加载
smallsharptools大约 11 年前
No thanks. Just learn to Storyboards already. It is it that hard and Xcode can help anyone who maintains this app. If it all in code it takes a lot of the and effort to understand what is controlling the layout.
评论 #7330884 未加载
k-mcgrady大约 11 年前
I think this is all pretty valid. However it&#x27;s important to point out the time savings using NIB&#x27;s and Storyboards. I do most of my UIs in code but occasionally use NIB&#x27;s when I&#x27;m doing layouts that include lots of objects that would be very time consuming to code.<p>Recently I decided to give Storyboards a try on a project. I was shocked at how much time it saved (no more pushing&#x2F;presenting view controllers, no more fighting tables to display custom cells) and for views that were only displaying information and a button to push another controller I didn&#x27;t even need to create a class as the push could be done in Interface Builder. I don&#x27;t think this approach is going to work well for all apps but when you&#x27;ve got something with a predetermined flow (a form for example) it saves a hell of a lot of time.
tejaswiy大约 11 年前
I see where this is coming from, but loosing the convinience of xibs + auto layout is too great to sacrifice for me. Just thinking of how much bigger my view controller code will get makes me shudder.
评论 #7330957 未加载
pavlov大约 11 年前
The big problem with Interface Builder is that, most of the time, its layout view bears no resemblance to what your UI actually looks like within the app. Deciphering the meaning of overlapping grey boxes in IB is often just as difficult as reading code. &quot;WYSIWYG&quot; it ain&#x27;t...<p>I&#x27;m working on a design tool that aims to fix this: <a href="http://neonto.com" rel="nofollow">http:&#x2F;&#x2F;neonto.com</a><p>Neonto Studio is a completely visual environment with support for vector drawing, video, smart guides, multi-device layout with full previews, and so on... There&#x27;s no coding involved -- this is an app for designers.<p>The tool generates readable iOS and Android code, and there&#x27;s no special runtime involved, so it&#x27;s ridiculously easy to take a few screens designed in Neonto Studio and drop them into a larger app.<p>It&#x27;s currently in alpha testing. I&#x27;m hoping to release a full beta in the next few months. If you&#x27;re interested in trying out the alpha today, I&#x27;d love to have your help -- just drop me an email at pauli &lt;at&gt; neonto dot com!
评论 #7333526 未加载
jfahrenkrug大约 11 年前
Don&#x27;t make the mistake of thinking &quot;Storyboard == Auto Layout&quot;. I really like Auto Layout and I think it is much easier to understand and read when declared in code. I strongly dislike Storyboards, though, for the 15 reasons outlined here: <a href="http://stackoverflow.com/questions/9404471/when-to-use-storyboard-and-when-to-use-xibs/19457257#19457257" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;9404471&#x2F;when-to-use-story...</a> However, some pains can be removed with this tool I wrote: <a href="https://github.com/jfahrenkrug/StoryboardLint" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jfahrenkrug&#x2F;StoryboardLint</a> It helps you to keep your IDs in code and in your Storyboards in sync.
chromejs10大约 11 年前
The way I&#x27;ve typically worked is static pages are allowed to be done in IB, but everything else is done in code. The reasoning is that doing things in code makes for much easier diffs and merges and also allows you to easily edit code in things outside of XCode (I personally prefer AppCode). Doing things in code makes thing a lot more explicit and can be setup so that the view code doesn&#x27;t have to interfere and floor the view controller&#x27;s code. The main problem is there are lots of things you can do in code that IB can&#x27;t do, but not much (if anything) the other way around. You should always be able to write your views using pure code.
Zigurd大约 11 年前
From an Android PoV I&#x27;m very skeptical of the benefits this. Unless you can round-trip between code and a visual tool, you are unlikely to get professional designers to touch a UI layout in code.<p>It&#x27;s hugely valuable to get designers to adopt the SDK&#x27;s design tools. Unless the designers on a project are obstinate about that, making that more difficult is usually a step in the wrong direction.<p>He also seems to be blaming the toolchain&#x27;s refactoring and the declarative UI XML for being difficult to manage. It is really that bad?
jarjoura大约 11 年前
Writing a large scale iOS app with 100s of XIB based views is equivalent to writing a Visual Basic app in my opinion. Suddenly your app is now dependent on -awakeFromNib and subclassing requires yet another another XIB. Just some reasons I prefer writing layouts in code (auto or not).<p>There are two camps of iOS Engineers in my experience. Those that feel IB makes your life better and those that feel the opposite. Both sides make valid points, but I vote for sticking with code as much as possible.
fyolnish大约 11 年前
Related project that I&#x27;m working on: <a href="https://github.com/fjolnir/Visual-Layout-Language" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;fjolnir&#x2F;Visual-Layout-Language</a><p>It&#x27;s a take on Apple&#x27;s visual format language that allows for defining a ui from scratch: <a href="https://github.com/fjolnir/Visual-Layout-Language/blob/master/Mac%20Demo/test.vll" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;fjolnir&#x2F;Visual-Layout-Language&#x2F;blob&#x2F;maste...</a>
nteon大约 11 年前
I do this too, and agree. The benefits and straightforward version control diffs are fantastic when working on a distributed team with multiple devs touching the UI.
puppetmaster3大约 11 年前
Same issue in Android. But this does not work in real life, because Designers.<p>Designers don&#x27;t do things in code, they use the UI panel designer. end of story.
评论 #7330937 未加载
评论 #7330935 未加载
stcredzero大约 11 年前
Using Tcl&#x2F;Tk, one did layout interactively at the command line. It was fast and very intuitive, especially in the way it used layout guides with easily understandable local &quot;laws of physics.&quot; However, it usually resulted in very staid looking UIs. Also, such a scheme would have to be modified to be able to deal gracefully with things like device rotation.
austinl大约 11 年前
<p><pre><code> I use UIView+Positioning, which exposes x, y, width, height, right, bottom, centerX, and centerY as both setters and getters. </code></pre> IB&#x2F;code aside, this is convenient. All of those properties are read-only by default, and that&#x27;s always seemed counterintuitive to me (though I&#x27;m sure there&#x27;s some purpose I&#x27;m missing).
评论 #7330915 未加载
nicholassmith大约 11 年前
I built an app using Storyboard last year, I&#x27;d do it again but only if the app was reasonably simple in scope. I think we ended up with about 20+ screens, and at that stage there was too much going on for it to be easy to work with.<p>Plus on a 13&quot; laptop on the move? Oof, hard to deal with. Only really could work with them easily on a larger monitor.
评论 #7333605 未加载
ja27大约 11 年前
When I first saw Interface Builder 25 years ago, I thought it was a neat tool for the programming-impaired. Maybe it&#x27;s finally getting useful but even with the latest iOS autolayout, I still prefer doing my own layout calculations in code most of the time.
fecak大约 11 年前
As an aside, I think it&#x27;s pretty impressive that a post by someone who graduated high school 8 months ago is being discussed and debated on the top of HN. He says he is looking for an internship, hopefully he will get some interest. Kudos.
onmydesk大约 11 年前
It looks to me like ios7 and auto layout is partly about running on many screen sizes. Makes you think about future devices and how your code will fare. Worth thinking about.<p>You might <i>need</i> to use IB.
评论 #7331510 未加载
blazespin大约 11 年前
You don&#x27;t want designers working in IB. You want them thinking colours, layout, UX, esthetics - not wrestling with XCode.
评论 #7331029 未加载
indubitably大约 11 年前
oh so you mean like css