Is there a way to add constraints into the xcode storyboard with code? - ios

I see that in the storyboard, under Constraints, it lists all of the constraints being used. I am currently doing video tutorials and I think it would be a lot easier to simply write down what I see instead of dragging and connecting everything right now. Is there a way to do this?

Yes. The respective classes to use are 'NSLayoutConstraint' and 'NSLayoutAnchor'. It is described in the programming guide "Auto Layout Guide".
BTW:
I just found another question that actually shows some code how to use it:
How to set UITextField width constraint to have room for a certain string

Related

What happens when attaching exactly the same NSLayoutConstraints to the same UIView multiple times?

The question says it all. Does it get ignored? Or does it just keep attaching the same constraint multiple times?
Also is there any performance issue if I do this?
I'm asking because the alternative seems to be keep reference to all constraints as they get attached and then later look them up to make this decision. It would be nice if I don't have to implement all this logic.
Yes, adding same(same NSLayoutConstraint object) constraints again, are ignored.
Since i haven't tried it practically but it will create problem if you need any sort of animation.
For example if you need to move your view with animation then you will have to change the same sort of constraint multiple time for each identical constraint which brings code pain.
Other than this problem, mutiple identical constraint doesn't really create conflicts but can cause performance issue if you have do this with many views in a single ViewController.

Placing The Section Header Above the Cell Contents of UITableView

**You may want to go 'Edit 2' section directly (at below), there you can find the last and current situation.
I can't explain the problem just with the words properly, so I'll try to explain with pictures. Here is the situation I have:
As you can see in the picture, the allignment of the header titles doesn't match with the contents of the TableViewCell. I want section header to be at the top of the values. Like, "Alış" string to be at the beginning of the values. To be specific, I want something like that:
Alış Satış
654.000 666.000
313.757 321.921
157.370 160.960
... ...
And so on.
I was referring to this link to make section headers. Of course, it is a simple doc, so I moved myself using "auto layout" and "update frames" and "add the missing constraints" from the Resolve Auto Layout Issues segment that storyboard has. Well, clearly I failed. Here is the tableView that refers to this problem:
So here, the second cell is used for the header, as suggested in the link I gave.
I'm not familiar with using constraint, that is making the issue more complicated to me.
Any help / suggestion would be appreciated.
Waiting for your assistance.
EDIT:
Here is the code I have to write header.
EDIT 2: Now, I'm really close and just need a little assistance
I was trying 'til the morning on this, I know it is a bit much time. Anyway. So eventually I figured it out and tried in iPhone 4s to 6 plus, they give the correct result, with a minor mistake. The answer was simple: playing with the autolayout to understand with reference. I recommend Apple's doc and Ray's doc.
Back to last problem, as you can see in the first picture, my last row of tableVieWCell's alignment is right which I want it to be left. So the last situation is at below:
**I worht mentioning that I know that the alignment may not be what is my topic, but I didn't want to ask another question. If it is really unappropite, you can warn me, so I can ask in another topic.
Please make seperate xib file with class file .h & .m and it's superview is UIView for section header. And design the same view which you have design for table cell. You can use autolayout also in xib file. This will work :-)

What is the correct way (or better place) to use topLayoutGuide/bottomLayourGuide in code?

Assume you do not want to use storyboards and instead programmatically create your viewControllers / views and autolayout them by code also. How do you properly make use of topLayoutGuide and bottomLayoutGuide? They are properties of the viewController.
I might be wrong, but I think the proper place to set constraints for autolayout are the relevant views and they usually are not supposed to have references to their viewControllers (again afaik).
Am I mistaken and the coding of constraints should be done in the viewController or if not, how do you get the information down to the views?
Please be patient I am learning ;-)

Create vertical tabbar for ipad

Today I tried to implement a vertical tabbar for iPad. I checked a lot of questions on Stackoverflow and other sites but couldn't find the appropriate answer. Here are what I found:
https://github.com/futuresimple/FSVerticalTabBarController
https://github.com/nst/SEVerticalTabBar
https://github.com/NOUSguide/NGTabBarController
In my opinion, the third component is great. However, I guess I'll have to customize a lot before I can use to create this following UI:
So do you know any components/libraries that match my UI? It would be great if anyone can suggest me a correct one. Thanks.
Try to use FSVerticalTabBarController. I have used it and it is easy to modify...
You can try this component IIViewDeckController. Its very light weight.
Usually it will be used for Stackview, left Menu kind of UI.
But you can do a small trick like below to achieve your design above.
Steps:
The component have left, right, top, bottom and center stack of controllers.
Use a UITableView on your LeftViewController to design your above UI.
Each index will act as a container of OneViewController.
Clicking one cell of your tableview will place the appropriate ViewController in the center controller of IIViewDeckViewController.
Its very easy, I guess you will like it. Just take some time to go through their examples.
Hope it will be useful
I translated futuresimple/FSVerticalTabBarController into Swift 5.
My repo is https://github.com/coyingcat/VerticalTabBar
Apple's doc Implementing a Container View Controller is great also

iOS - dependent constraints console output with autolayout

I am making an iOS app that uses popovers on several occasions. I get the following output message to the console every time one specific popover is dismissed (but not when any of the other popovers are dismissed):
All dependent constraints should have been removed from the engine and also from the view's list of dependent constraints
I have searched Google and this site for that phrase and nothing has come up. I am not sure why it would happen for only one popover and not others. Would it have something to do with its view controller? The popover itself? I am an autolayout novice, so there may be something simple I'm missing. Any ideas?
I too had this issue i solved it by removing the Use AutoLayout checkbox from the XIB, the auto layout XIB is present in the file inspector option.
Hope this helps

Resources