Is there a shortcut for (self.frame.origin.y + self.frame.size.height)?

I am writing too much of this code:

self.frame.origin.y + self.frame.size.height 

Is there a shortcut for this? Something like self.frame.y_plus_height ?

If so, I'm not sure if this is good news or bad news for the whole time that I wrote the full sentence.

+4
source share
3 answers

This should do the trick:

 CGFloat res = CGRectGetMaxY(self.frame); 

The documentation can be found here .

EDIT: as explained by rob mayoff (see comments) this is a bit more expensive than just summing origin.y + size.height in the code.

+10
source

There is a great UIView category created by nfarina .

Check here .

Using this category, you can get "max Y" or "bottom" of this form (where self is the view):

 CGFloat maxY = self.$bottom; 

This category is really awesome because it also makes it easy to set frame properties. In the example, to move the view to the right by 3 points, you can do this:

 self.$x += 3.0f; 
+3
source

I understand this is an Objective-C request, but for those using Swift and stumbled upon this question, you can simply create the CGRect extension:

 extension CGRect { func maxYinParentFrame() -> CGFloat { return self.origin.y + self.size.height } } 

Then you can use it anywhere.

 let mySize = myView.frame.maxYinParentFrame() 
+2
source

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


All Articles