How to rearrange views with iOS Auto Layout - ios6

I'm trying to wrap my head around auto layout. I think I get it for the most part (probably missing something), but how would i set up the views pictured below?
Without auto layout, I would just test for orientation and then re-set the frames of the blue and purple views (and also the white ones probably). In auto layout, I'm not sure yet how to approach this. Would I need to remove and replace the constraints, so that in top/bottom constraints between these views in portrait orientation, I would have leading/trailing between them in landscape? Or is it easier than that?

Related

How to make some divs more responsive than others for different aspect ratios using susy 2

Since it's a bit difficult to explain, I did a mockup to get across as much as possible visually:
http://sassmeister.com/gist/70624a740b1ca4ae7764
(If there's a better way to share a sass gist, let me know. First time using it)
Basically, this is the layout I want for a tablet in landscape mode. What I'm trying to do is make sure it fits perfectly on different tablets with different aspect ratios. Some things are fixed. The main content area is a 16x9 video, so that aspect is locked.
I have the header and footer (main column only) fixed right now as they need to be for portrait mode, but I could bring them into the regular flow if it's helpful for tablet landscape. Anyway, it's all basic responsive right now via susy2, and the sidebar is totally separate so it can scroll independent of the main content. What I would like is for the whole main area including header and footer to fit perfectly with even margins above and below vid, but then have the sidebar column change it's width to match the tablet.
So... if the tablet is wider, the teaser thumbnails go out to 16x9 ratio. If the tablet is narrower, the main column remains unchanged, but the teaser thumbs narrow down to squares.
If it's easier first to just figure out how to responsively shrink the right column only, so the aspect of thumbs is unchanged, that's ok. I just don't want the overall layout to get screwed up on one device vs another because of aspect ratio, so main focus is that the header hits top, footer hits bottom, main vid fits perfectly between them, then sidebar responds to fill in the rest (within reason).
thx for any input. First time making a website here, so lots to learn.
ps. I had vertical scroll enabled for the right column, but disabled it (by adding extra letter to to the scrolling class in scss column) since it's not actually letting me scroll. Not sure if that's because there's no actual content, or it doesn't recognize the empty padded cells as something worthy of scrolling.
You're biting off a lot for your first website, but Sassmeister is a great way to show what you are doing. I approve. :)
One of the problems you'll find is that CSS don't have the concept of aspect ratio built in, so the sort of layout you are attempting is non-trivial. CSS is best at handling widths, and letting everything overflow vertically. It takes some effort to make it handle height well.
If you can get away with it (depending on browser support), your best option is to use flexbox. Flexbox should make this much easier, but doesn't ave a lot of support yet. You could consider table-cells, which have more support, but can be harder to control.
In any case, you should ignore most of Susy for this — at least in laying out the large sections. If you want Susy to help you with grid calculations, ditch the mixins and just use the span() and gutter() functions to help you set widths. Something like this:
.flexbox {
flex: 1;
flex-basis: span(3);
}
.tables {
display: table-cell;
width: span(3);
}
// NOT THIS
.no {
#include span(3);
}
You can go back to using Susy mixins for simpler bits, like the items in the top navigation.

IOS6 Constraints using Autolayout

I have an app that can be rotated, so I need to deal with portrait and landscape orientations. Additionally, users will be allowed to use pinch gestures to change the scale of views. Here is the basic hierarchy of the views.
mainView is a subview of self.view (from the context of the main view controller). It is a UIImageView, although the image part of it is relatively unimportant. In any case, this is the view within which the rest of the views in this discussion are placed as subviews.
The first is what I call the board. It is the view on which items are assembled by the user. These items are themselves image views.
Additionally, there are what I call palettes. These are simply views that can be resized and scaled by the user. Additionally, the image views just mentioned can be dragged from one palette to another or to the board. The palettes can be thought of as work space for the user. When they are finished their work, they place their assembly onto the board.
So far, I've been working with the app where the board is part of autolayout but the palettes are created programatically as needed. This is good because when the user rotates the device, autolayout automatically places the board appropriately. At least it did until I wanted to add pinch scaling to it.
Autolayout has the following constraints set to it in interface builder:
Leading, top, trailing, and bottom all set to superview default.
When the user scales the view, the result is that it sort of sticks to the upper-left corner of the screen. I'd rather have it retain the center.
I tried changing this programmatically by adding the following code to the pinch gesture recognizer for this view:
if (self.pinchView.tag == TAGBOARD) {
[NSLayoutConstraint constraintWithItem:self.pinchView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.mainView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
}
but this seemed to do nothing. I'm guessing it's because it conflicts with the IB constraints. Is there something else I can do to make this work with autolayout? Or should I just do it all programmatically like I do with the other views?
In this code example, self.pinchView is the view on which the pinch gesture is applied to. For the sake of this discussion, it is what I've called the board. The self.mainView view is its superview.
The part we're not seeing in the code is something like
[self.pinchView addConstraint:yourNewConstraint];
I can see where you create the constraint, but not add it to the view. If you want your constraint to win you'll need to remove the other views or make sure the new constraint has a higher priority.
If your view should be centered, try adjusting the constraints in the storyboard to pin the width and height to the default and then aligning horizontally in the center. That should satisfy autolayout and replicate what you're trying to add, then in your pinch recognizer you can change the constant of the width and height. Be sure to drag your width and height constraints into your controller to create an outlet so that you can adjust them during the pinch gesture.

Keeping a UILabel centered with changing contents and AutoLayout

I have 2 views. A MapKitView and a UILabel. Auto Layout is on.
I want the MKView to take up the whole screen (this seems to work fine) and the UILabel to remain the auto distance from the bottom of the screen but centered horizontally. The UILabel contents are changed programatically and afterwards I am calling sizeToFit.
After calling sizeToFit on the UILabel then it is no longer centered - the width seems to be adjusted only from the right hand side (trailing edge) keeping the left hand side in a constant position.
My understanding is that I should be able to achieve what I want only by setting the right constraints in IB?
The constraints I have applied to the label are:
Height Equals: 32
Width Equals: 166
Bottom Space to: Superview Equals: Default
Align Center X to: Map View
Only the last one, "Align Center X" is a user constraint - the rest being IB supplied.
I have tried a number of things, though none with much understanding (presumably my problem!).
I have called setTranslatesAutoresizingMaskIntoConstraints: NO on the label.
I have set the label's autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin. I have tried setting preferredMaxLayoutWidth to random values. Calling needsUpdateConstraints on the label. I tried putting the call to sizeToFit in viewDidLayoutSubviews.
I have come to the conclusion I need a little more understanding how I should be approaching this. Is sizeToFit not the thing to be calling when Auto Layout is on? Do I need to prod Auto Layout in some way after calling sizeToFit? I notice that rotating the simulator seems to center the label so prodding sounds a possibility?
I have re-read the Apple Auto Layout guide but I'm afraid I'm still not sure what I am doing wrong and the debugging tips suggested (visualizeConstraints etc) don't seem to work on iOS.
Thanks for any clue you can spare.
My idea about having to prod appears to be correct. After calling sizeToFit I needed to call [label.superview setNeedsLayout]. Whilst this works I'm not really sure why I need to do this manually rather than the label marking the layout as dirty when it's size changes.

iOS 6 AutoLayout Scale and Translate Animation

My aim is to have 3 images shrink, grow, and move along a horizontal axis depending on selection. Using Auto Layout seems to make the images jump about as they try to fulfil the Top space to superview / Bottom space to superview constraints.
So to combat this I have put all the images inside their own UIView. The UIView is set to the maximum size the images can grow to, it is centred on the horizontal axis. So now all the images must do is stay centred inside their corresponding UIView. This has fixed my problem as the UIViews perform the horizontal translation, while the images shrink/grow inside while remaining centred. My question is - is this the correct way to do this? It seems very long and like I am perhaps misusing the ability of Auto Layout. I have to perform similar tasks with more images and so any advice is welcome! Thanks.
I've just written a little essay on this topic here:
How do I adjust the anchor point of a CALayer, when Auto Layout is being used?
Basically autolayout does not play at all well with any kind of view transform. The easiest solution is to take your view out of autolayout's control altogether, but alternatively you can give it only constraints that won't fight back against the particular kind of transform you intend to apply. That second solution sounds like just the sort of thing you're doing.

Remove all extra space around a button with an image?

Alright, try as I might, I cannot for the life of me get rid of this tiny little border around my buttons.
Edit: I should mention, in case I didn't make it clear, these are buttons with an image on them, set to flat with the button sized to the image.
Images below:
Number one, I can't for the life of me get these borders to GO AWAY. I've checked everything I can think of. They're:
flat
border 0
no margins
no padding
manually sized to the size of the image (75px)
in a table layout where the columns are all:
manually sized to the width of the image (75px)
borderless
Nothing seems to really "work" to get rid of these. If I size the columns down to be 74px instead of 75px, most of them go away, but a few remain. I've triple and quadruple checked the images, and they don't have anything that I can pick up on that should be causing this... no transparency around the borders, definitely no border that looks like that.
Which leads me to the second problem:
Settings button when dialog is small...
Settings button when dialog is stretched out.
Settings button is also in the same table layout panel.
I've checked all the settings on the table layout panel as well.. I can't find any padding or margin or anything settings that suggest this should be happening.
Does anyone have any experience with this? What am I missing..?
Simple solution: using directly a PictureBox as if it was a button. You can change your image on mouse over or mouse click.
Have you tried a Toolbar/strip/whatever it's called these days? Probably not going to help as I believe it pads on your behalf, but worth a shot.
In the end you can toss the buttons in the trash and write your own control. A single control that manages N buttons will work well here.
I don't understand your second problem. What's the problem? It'll be fixed if you roll your own control anyhow.
While not a fix for the spacing issue, as a workaround you can make that gray gradient currently "behind" the "tabs" and control panel image into a BackgroundImage for the TableLayoutPanel using BackgroundImageLayout of Stretch. While not fixing the spacing issue, it would make it unnoticeable.
Writing a winforms control has its challenges (experience speaking here). I would agree that that is whats needed however. Depending on your project you may consider using XAML and WPF. It provides that fine detail you seem to be looking for in you application.
There are ways to host XAML controls in a winform app, but if you went this route it would be best to create a native WPF application. The reverse is also true (winform controls in a WPF app).
Did you check if the image has transparent pixels around the graphic pixels you want?
May be a simple crop solution.

Resources