📜 ⬆️ ⬇️

How to beat Excluded Constraints with RubyGem



During the development of iOS / OSX applications, you certainly have to use Interface Builder and Auto Layout. In applications with complex interfaces and in applications that have been in development for a long time, there is often a problem with a large number of “excluded constraints”. Storyboard loads unreasonably long, and during the transition between scenes, you can catch up with coffee.

But, it would seem, the interface of all controllers is just as uncomplicated as in the release six months ago. What happened? For those who have used Size Classes, it’s immediately clear that the interface is not as simple as it seems. It behaves differently in different orientations and on different displays. He does this because some constraints apply only in a certain situation.
')
This means that in other situations these constraints are not used. Xcode Interface Builder has a feature that helps in the development of complex interfaces and interferes with the development of simple ones. If you add constraint to some UIView and then remove it from the Xcode Inspector (and most often it is the most convenient way), Xcode will not remove it, but will only add it to the list of unused ones in case it is needed for another display or orientation.

Sometimes, if you expand all constraints groups, you can be terrified of what is happening in the storyboard file:


Do not want to watch this? Welcome under the cut!

More than half of the constraints are in the “basket” and wait until they are added to a specific display or orientation or deleted. Until then, precious seconds of downloading the file and the remaining nerve cells of the developer are being eaten.

What to do?
You can open all your storyboard- and xib-files in turn, open all UIView heirs in them and, making sure that this constraint is not used for Size Classes, remove them using the Document Outline

And you can provide this boring, not having the right to make a mistake work smart Ruby-script. So, we need to find all the storyboard and xib files in the project:
``` files = Dir.glob("**/*.{storyboard,xib}") files.each{ |file| cleanupConstraints(file)} ``` 


Done, now in these files you need to find the excluded constraints:

 ``` require 'nokogiri' def cleanupConstraints(file) f = File.open(file) doc = Nokogiri::XML(f) f.close excluded = doc.xpath('//exclude') end ``` 


You also need to not forget about the Size Classes and leave all the constraints on the ground, which are included in at least one display / orientation combination.
 ``` require 'nokogiri' def cleanupConstraints(file) f = File.open(file) doc = Nokogiri::XML(f) f.close excluded = doc.xpath('//exclude') included = doc.xpath('//include') result = [] excluded.each do |node| found = false for includedNode in included if node.attr('reference') == includedNode.attr('reference') found = true break end end if !found result.push(node) nodeID = node.attr('reference') constraints = doc.xpath("//constraint[@id='#{nodeID}']") result += constraints end end if result.count > 0 f1 = File.open(file, 'w') result.each{ |node| node.remove } f1.write(doc.to_xml) f1.close p "removed #{result.count} constraint(s) from #{file}" end end ``` 


Small win, but how to use this script? The answer is simple - create a RubyGem.

There are several ways to create a gem , I used the guides.rubygems.org/make-your-own-gem described here.

Now you can clear the project (or even all projects at once!) By installing Gem
 ``` $ gem install constraintClean ``` 

In the directory of your project (or in the directory with all your projects) by executing
 ``` $ constraintClean ``` 

And it is beautiful!

github.com/kohtenko/KOConstraintClean

Source: https://habr.com/ru/post/262179/


All Articles