Swipe 2 views postions in autolayout during run time - ios

I'm looking for customized solution.
I have these two buttons with their constrains
I want to swipe their places During run time.
I've tried the translatesAutoresizingMaskIntoConstraints option but it's misbehave in many scenarios.
also I can't just outlet some constrains and change their constants during run time because the two buttons are related to each other
What to do ?

If you just want position of both button to change and not to perform any animation, then you can simply swap the background color and title text of next/back buttons. You would also need some extra logic to handle actions of both buttons after the swap.
In that way they will appear swapped

Related

UIButton with background image and title using auto layout

I currently face a problem when using UIButton. I have the background image designed by others like this:
background image
and I need to place the title for the button right after the white vertical bar in the background image. I tried to set left edge for the content but since I used auto layout, the frame will be different with different screen size(3.5", 4.7"...).
Is there a way to put text in the position related to background I want with auto layout.
Thanks
I personally would split the left side of the background image from the right side. This way you can have two UIButtons next to each other with horizontal space constraint of 0. The right side of the button will have to be placed inside a UIImageView so you can set the image as a property of the view rather that the button's background. You don't have to do this of course, but I prefer this solution as it is easier to manage across different screen sizes.
Here is a visual representation of what I explained above:
Separated Views for Single UIButton
You will then need to route the touch events of both buttons to run the same method or function, just so that both the right side and the left are clickable.
Note: I'm not sure exactly what you had in mind for the highlighting of the button, but this will have to be tested and adapted to get the desired effect.

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.

Hiding button leaves whitespace instead of dropping down top button in ios

I am hiding the second button below, but I want the top button to drop down. Is there a way to do that where it has a relative layout.
Yes you can do this. One of my project I had to do this. If you are using autolayout then you can not manually change your buttons frames. You can either have to disable autolayout, or modify the constraints programmatically. I followed the first one.
In your case, if you want the first button to take the position of the second one (which you are hiding) then take the frame of the second button and assign it to the first button.
Hope this helps. :)

How can I dynamically add/remove a button in a UIView and reposition the buttons below it and vertically resize the view?

I have a UIView, defined in a nib, and I need to be able to show/hide a middle button in that view. When I show the button, I need to reposition the two bottom buttons below it, as well as make the view taller to make room for everything. When I hide the middle button, I need to move the two bottom buttons up and vertically resize the view to make it less tall. I do NOT need to animate any of this since it the changes will never occur while the view is visible to the user.
I'm new to iOS and I'm used to using Autolayout, but I can't use Autolayout in this case to handle this automatically, so my current approach is to hardcode the frame position and dimensions for the two bottom buttons for each of the two different situations. I'm also hardcoding the two different frame sizes for the view itself. In viewDidLoad, I determine if I need to show/hide the middle button and set the frames for the view and bottom buttons appropriately. This works, but if feels hacky. Is there a better way I should be doing this?
Thanks in advance for your wisdom!
You don't need to hardcode your frame sizes in viewDidLoad. The only thing you should take care of is determining that whether you need to show you middle button or not. Within the implementation file where you are allocating your UIButtons, check if the middle button has to be shown, if Yes allocate it, if not then don't. The frames of two buttons and the view should contain a factor which can set/size them accordingly.
You'd basically be managing the Auto-layout programmatically. And if you're not even allowed to that then whatever else you'd end up doing would pretty much be a hack.

slider control similar to ipad default calendar app

I would like to know how to implement the slider similar to the one in the Ipad default calendar application. I have attached the image below
If you see at the bottom, it acts like a slider which allows us to select any month either by just pressing it or sliding to it.
It would be great if anyone could tell me the name of that control. I tried using UISlider but I see that it allows only 3 options:
setThumbImage
setMinimumTrackImage
setMaximumTrackImage
If that control is indeed a slider control, could anyone tell me how I would be able to insert multiple images/ text
Thanks
It may be more complex than you'd like, but you could make your own:
Make a background that has UILabels for the dates
Make a selection box
In the UIViewController you could put something such as:
touchesMoved -
Make the selection box's x value equal to the touch
touchesEnded -
Make the selection box's x value equal to the touch's last x value
The touches moved would allow dragging of the selection box, and the touchesEnded would allow tap selection.
Then you would simply animate the selection box to the x value.
(This is an extremely simplified version, but you could do this pretty easily.)
This can be achieved by using UIScrollView. Add UIButtons programmatically having background images and text as well on them as per your requirement.
You can refer tutorial1 tutorial2 in which on screen only 1 page(Image) is displayed. You need to do some calculation for applying same logic to fit your requirement. i.e. Your scrollview will be smaller(in height) and bigger(in width) as displayed in image, you will be adding UIButtons in spite of images and most important at a time displaying more than 1 item but it won't be difficult. The Main part is only programmatically scrolling.

Resources