NSWindow shrink to fit content with padding - swift

I'm new to cocoa development and wondering what the right approach would be to the following:
I have a window with controls who's size is dynamic. In other words, I don't know the width of the window in advance. I also want to give the window a padding of 15px. In the CSS world I would simply pad a div and make it inline to accomplish this. I can't seem to figure out how to do this with NSWindow/NSView.

Question is too vague to be sure, but IFF I'm understanding you, you want a subview of the window to resize itself to always be 15pixels from the left and right sides of the enclosing window (for example).
here are the two constraints (as set up in Interface Builder, but you can do the same thing from code):
That's one view whose width is set to adjust while keeping the left and right padding to the window frame at 15 pixels (points actually, but don't worry about that).
As your view hierarchy gets more complicated you'll use different techniques for different pieces of the layout. Which is why matt was suggesting that you haven't been specific enough to actually answer a question. (What specific part of what you are you trying to do isn't working and what did you try that didn't work?). I've shown you the first step of the simplest outer layer above and it reflects a principle that carries down through your view hierarchy, but there are other cases - two items with the same width, three items with the same horizontal space between them, and so on - that require different techniques.
It sounds reading the auto layout documentation all the way through would be helpful for you. It talks about different types of relations (Equal is what I used below) and some on techniques. Unfortunately Apple's documentation situation is a bit of a mess at the moment which makes this more difficult than it should be (sigh).
What used to be the definitive auto layout guide is "unmaintained" and the place that documentation seems to be heading is incomplete and certainly less accessible. The most useful part for this context is the NSLayoutContraint documentation, though that links to the "Unmaintained" document noted above (sigh).

Related

Auto layout if orientation is not an issue?

I am currently trying to get the hang of Auto-layout in Xcode, and with it, i have found some problems.
Now, i don't have a 100% grasp on how auto-layout works exactly, so as i learn on it might become easy, but i think it is very hard to be honest. I am usually making games, and of thus, i programmatically add a lot of views to the screen. Now, auto-layout seems to mess everything up when i do this, so i constantly have to make sure that every single auto-layout feature is just right, and there are a bunch!
So, if you do not want to support other orientations, is auto-layout really needed? i mean, if i am making a game for the iPad and using landscape mode only, is there any reason for me to use auto-layout?
Thanks on advance
A couple of thoughts:
Auto layout is not mandatory.
But auto layout provides a mechanism for dealing with situations when views change sizes and/or locations:
changing orientation, as you pointed out;
supporting different screen sizes (e.g. iPhone 3.5" screen vs 4.0" screen);
controls whose size changes based upon content (e.g. a UILabel whose width expands to fit the text or whose lineCount is set to zero, meaning that its vertical will grow based the number of lines needed to fit all the text); with auto layout you can establish dependencies between controls so that, for example, as one control changes its own size/location, others can move/resize accordingly (e.g. to stay aligned with or make room for the other control); and
automatically update contentSize for UIScrollView objects.
Auto layout requires a little time to gain proficiency, but for dynamic, complex user interfaces, it can save you from having to write code to manually change layouts of views programmatically. But if you don't find yourself doing this sort of code, then you don't necessarily have to use auto layout.
I should acknowledge that there are some tasks that are trivial in a non-autolayout environment (e.g. animation of moving views), doing the same task in auto layout can be frustratingly complicated. But if you have complex interdependencies between views (either between the superview and its subviews or between sibling views), auto layout can be useful.
Autolayout simply provides rules for the views how they appear in all orientation.It is upto the user to define how to use it or you may follow the struct and spring model .
The major difference between the two is that there no dimensions involeve in the struct and springs while autolayout says the dimensions and values in whch views should be rearranged
An excellent beginner tutorial fo autolayout

Is this programming paradigm vital to my success?

I'm currently making apps for iOS, and I had a quick question about making UIViews. In the process of designing a UIView, I was wondering if everything should be based off of the bounds of the rectangle that contains my view.
For example, the one I'm currently working on is designed as a header that only occupies the upper 25% of the screen. Despite this intention, should I still design the code so that if the view were to occupy the entire screen , it would still work?
To provide a scenario, lets say I need to draw a line. Should I just draw it 20 pixels across, or should I always go 30% of the width of my rectangle.
I understand the concept of reusability, but if I'm designing this view only for this particular purpose, is it acceptable to make it somewhat rigid in nature?
Designing for change and reusability is always a good practice. However, as you have also realized, it introduces overhead which can sometimes outweigh the benefits of the flexible design.
I would say that it is fine to hard code some values if the view is used only for a particular purpose with a particular size. It is fairly common to create fixed size images for UI components which is just like using fixed size values in your code. Nevertheless, it is a good practice to use constants for all your hard coded values and collect all of these at a centralized place of your code e.g.:
static const CGFloat centerLineWidth = 20.0;
This way you can relatively easily reconfigure your views if something needs to be changed.
Finally, if there is even a slight chance that the view might be used with different sizes, you should go for the flexible design. You can also mix these concepts, e.g. create a view which is designed with flexible width in mind but its height should be a fixed value.

Finding a free space within current bounds of view on iOS

I have an infinite scrollview in which I add images as the user scrolls. Those images have varying heights and I've been trying to come up with the best way of finding a clear space inside the current bounds of the view that would allow me to add the image view.
Is there anything built-in that would make my search more efficient?
The problem is I want the images to be sort of glued to one another with no blank space between them. Making the search through 320x480 pixels tends to be quite a CPU hog. Does anyone know an efficient method to do it?
Thanks!
It seems that you're scrolling this thing vertically (you mentioned varying image heights).
There's nothing built in to UIScrollView that will do this for you. You'll have to track your UIImageView subviews manually. You could simply maintain the max y coordinate occupied by you images as you add them.
You might consider using UITableView instead, and implementing a very customized tableView:heightForRowAtIndexPath: in your delegate. You would probably need to do something special with the actual cells as well, but it would seem to make your job a little easier.
Also, for what it's worth, you might find a way to avoid making your solution infinite. Be careful about your memory footprint! iOS will shut your app off if things get out of hand.
UPDATE
Ok, now I understand what you're going for. I had imagined that you were presenting photographs or something rectangular like that. If I were trying to cover a scroll view with UILeafs (wah wah) I would take a statistical approach. I would 'paint' leaves randomly along horizontal/vertical strips as the user scrolls. Perhaps that's what you're doing already? Whatever you're doing I think it looks good.
Now I guess that the reason you're asking is to prevent the little random white spots that show through - is that right? If I may suggest a different solution: try to color the background of your scroll view to something earthy that looks good if it shows through here and there.
Also, it occurred to me that you could use a larger template image -- something that already has a nice distribution of leaves -- with transparency all along the outside outline of the leaves but nowhere else. Then you could tile these, but with overlap, so that the alpha just shows through to the leaves below. You could have a number of these images so that it doesn't look obvious. This would take away all of the uncertainty and make your retiling very efficient.
Also, consider learning about CoreAnimation (CALayer in particular) and CoreGraphics/Quartz 2D ). Proper use of these libraries will probably yield great improvements in rendering speed.
UPDATE 2:
If your images are all 150px wide, then split your scrollview into columns and add/remove based on those (as discussed in chat).
Good luck!

Scrollwheel as UISlider replacement

How can I integrate a Scrollwheel into my application?
I'm currently using Sliders but have found them to be sometimes difficult to control exactly (for example with a linear scale from 0% to 100%). I guess they weren't designed for that purpose and are meant to be used for cases where not pitch perfect control is ok (Volume Control and the likes). However, I really need an exact way of inputting data (other than TextFields, they won't work in my case).
I figured that a Scrollwheel kind of UI Element would be perfect for me. Are there any opensourced Scrollwheels available that would fit my needs?
Horizontal, just like Sliders
Variable Start and End Values
Variable Scale
Small in height
Pretty :)
I tried using the Picker but that didn't work for me since it shows it's values inside of it, which makes it both big and not pretty to look at when used multiple times inside of one View.
If there's nothing available that fulfills my needs (described above) could someone please give me a hint on how to start effectively with creating such a UI element? Thanks!
I've finally found something which fits my needs :)
OBSlider, a subclass of UISlider which allows variable scrubbing speeds – it imitates the behavior seen while scrubbing in iPod.app.
Fulfills all my needs:
Horizontal
Variable Start and End Values
Variable Scale
Small in height
Pretty :)

How can I maintain relative sizing in Perl/Tk PanedWindows

I'm working on a Perl/Tk GUI. It will have three main areas. Two of them side by side on top and then another one below the two.
I could just use grid geometry management. The upper two would have a row weight of 2. The lower one would have a weight of 1.
This would be good for the starting position, but the user needs the ability to adjust the sizes.
Looking at the Tk documentation, PanedWindows can also have a weight, but I can't figure out how to access it.
As I have it now, with my Paned frames, the upper and left children are minimum size, everthing else fills the area below and to the right. If I adjust the main window. only the lower and right windows are resize. Worst of all, I can resize the window and make some of the children disappear.
I want to maintain the current relative sizes.
How do I do this? I'm not tied to paned, grid, pack. Whatever works.
Sounds like you are using frames in Tk. While I have never used Perl/Tk, I am kind of savy html which Tk is probably based on html. So, you might want to look into frames and framesets at Frames in HTML documents on the W3C site.
It sounds like you need 1 frameset with 2 frames for the top and another frame for the bottom.
I hope that this helps.