Autolayout for hidden/shown object ios - ios

I have a UIView which contains, let's say, 7 objects (UILabel). The size is fixed.
In some case, I must hide some of the objects. I do it with setHidden:YES.
The problem is that the appearance of the view is not very appealing, because the position of the objects is fixed. It could be better if the objects can freely move up and down when space is available...

I found a solution, I am not 100% comfortable with. The technique use a for loop over all the objects/view we want to slide up/down. The solution is here

I build category to update constraints easily:
To hide uiview, just do it:
//Hide View
[myView1 setConstraintConstant:0 forAttribute:NSLayoutAttributeHeight];
Answer here:
Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one

Related

Custom Page Control with Paging Scrollview

Instead of the standard dots that Apple provides for a page control, I want to have something like this. However I don't want it to be in an infinite loop were you can circle around. All I care about is the scrollable text on top, not the parallax image.
Currently I have a paging scrollview that contains three view controllers so my custom page control will have only three words: Main, Today, Settings.
The way I see this being built is the following:
Subclass UIView and insert three UIButton's and evenly space them. The title of the buttons will be Main, Today, Settings.
Insert this UIView as child of scrollview (or maybe not)
Make UIView the width of the iPhone screen
Not sure about here now -> as you scroll the scrollview shift the UIView on and off the screen so that the UIButton will be centered in one of the view controllers in the scrollview.
Am I on right track or does anyone have a demo to this?
Yes. You are on right track. You can use scrollView for this exact purpose. You have to make use of scrollViewDelegate methods for this. The below link will explain you how to do that.
How to make Paging with scrollView.

Xcode IB: UIButton hidden but have still buttons space

I have the following issue in my iPhone app.
I have 4 buttons in my IB also linked to my UIViewController (IBOutlet)
When I for example hide the second button which is AfvalSoorten with [self.btnAfvalSoorten setHidden:YES]; then it disappears, that's what I want, but I still got the button space when I debug the app on the simulator.
How can I get rid of that?
Below there is an example.
Is there an option on the Storyboard for the buttons to clip together?
You should use autolayout. Otherwise it's a nightmare with the new screen sizes.
With autolayout you can do what you ask programmatically: setup the buttons with certain constraints and then when you decide to hide the button remove the constraints that are not needed. It's flexible and powerful but not the easiest way for a beginner.
One simple way to do it is with additional constraints. For instance, if you have buttons 1, 2 and 3 (see screenshot), and you plan to remove button 2, you can add an extra constraint between 3 and 1:
That constraint should have less priority (250 in my example) than the others (1000 by default). That mean that the constraint won't be applied when button 2 is in place (with higher priority constraints).
Then, remove the button instead of hiding it.
[self.button removeFromSuperview];
When you hide the button it still considered by the layout system to take decisions, and it makes layout more complex. If you want to keep the button around make sure it's using strong modifier in the property declaration.
A better approach for above scenario - You dont need to set any autolayout or frames :)
Use UITableView and create custom cell with UIButtons in it.
Set UITableViewCellSelectionStyle to None
Here your button background is same for all cell
Create an array with above button titles
When ever you want to hide buttons just remove it from array.
The modern preferred way of doing so is to use Stack Views. Great tutorial. Requires iOS 9.
You'll find the icon of stack view in the Auto Layout toolbar at the bottom right of the storyboard canvas.
You had just make it hidden.
You have to set the frames according to your need.
OR
You can set autolayout.
maybe there is a solution with autoLayout in IB but I'm not sure about that. Programmatically you can add all your button to an array in order. and whenever you hide a button or not you loop trough the array of button and each time you find one that is not hidden you set the y coordinate on the frame to a a value and you increment this value by what you need so the next not hidden will be placed according to the last position used.
You can add the buttons programmatically -> you will have the array
of Btns and method to add array to the view controller.
You can play with the Constraints and set Height Constraint for 2nd button when hide it to 0 but all buttons should be connected with the constraints in this case.

How Do I Create A Simple Scroll View That Scrolls Through Static Images?

Perhaps it's something with Xcode 4.5+ or iOS 6 that makes my research fruitless so far, but...
Apple's own advice hasn't worked for me. I set the contentSize to something different, and it causes no changes.
I found this wasn't unique to me, but the answer in that question 1.) does not inform me about the problem and 2.) I'm left wondering if it's really impossible to create a simple, non-inheriting example of UIScrollView. Thus, this question.
A better answer's code was tried within viewDidLoad and viewDidAppear, but to no avail.
So what gives? Is it possible to create a simple, image-filled view that allows scrolling? Say... something as easy as this basic project I set up on github -- but working?
Create and initialize a UIScrollView
Add it as subview to the view of viewcontroller
Make an array of image names
In a loop create UIImageView and add the images and add imageviews into scrollview considering the width of images, set the frame of imageviews
Number of images is known and the width it consumes can be calulated from the loop itself use it to set the contentSize of the scrollview

How do I create a view in iOS 5 which can scroll in any direction, like a map?

How do I build a freely scrollable view on iOS 5? I mean, I need to build a view which can scroll from left-right and top-down (any direction) and it will stay there. Inside this view, I need to place different kinds of objects.
(Imagine scrolling like a map. - I am using Storyboard.)
Yeah, UIScrollView.
And don't forget to set the content size !! (every noobs - including me - are always asking the same question, why my scrollView does not work?)
=> [scrollView setContentSize:CGSizeMake(1000, 2000)], for example.
EDIT :
I just saw your comment. I don't understand what you call "on top".
I made an app with custom maps, and I used an UIScrollView to make the job: it was pretty good at it.
You just have to add each scrollable element as subviews of your UIScrollView. And set the contentSize to fit to the subviews' frames, of course.

iOS UIImageView Layering question

I am sure this question has been answered in numerous places but I have no idea what keyword to search for to answer this question. If anyone can link me to the answer that'd be great.
I'm not sure how to control which UIImageViews (which are all subviewed to the same view) cover the others visually. In other words, if I subview a UIImageView to the same location as another UIImageView, the 2nd one that was added is always covering the first one. I'm not sure how to do this either in the UI editor or in code. So far I've been adding them in the order I want them to show up but I can't always do that. Especially if I need to set UIImageView that's an IBOutlet and I need it to be on top of all the UIImageViews that I add programmatically.
Can someone show me how to do this programmatically and possibly how to set values in the UI editor? Thanks.
UIView has several APIs to deal with these issues:
When adding a subview:
insertSubview:aboveSubview:
insertSubview:atIndex:
insertSubview:belowSubview:
Moving subviews around in their z-ordering:
bringSubviewToFront:
sendSubviewToBack:
exchangeSubviewAtIndex:withSubviewAtIndex:
More details on all of these can be found in the UIView documentation (of which UIImageView is a subclass.)
This is done at the UIView layer and is not specific to UIImageViews.
Views can be programmatically 'relayered' with the following UIView methods:
– addSubview:
– bringSubviewToFront:
– sendSubviewToBack:
– removeFromSuperview
– insertSubview:atIndex:
– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– exchangeSubviewAtIndex:withSubviewAtIndex:
To do it to the XIB in Interface Builder, reorder the views listed on this view, bottom row is the frontmost object:

Resources