What is the method “backgroundRect(forBounds:)” of UIButton used for?


I found out that UIButton has a backgroundRect(forBounds:) function, but I can not understand where and how can I use it.

I also cannot understand how can I use the contentRect(forBounds:), titleRect(forContentRect:) and imageRect(forContentRect:) methods. Please, explain me.



But all the same I can not imagine a situation in which I will need to address this function


Then imagine this: a button that appears to shrink a little in response to being tapped, as feedback to the user, growing back to its normal size when the user's finger is no longer on the button. How would you do it? Here's an elegant way:

extension CGSize { func sizeByDelta(dw:CGFloat, dh:CGFloat) -> CGSize { return CGSize(width:self.width + dw, height:self.height + dh) } } class MyShrinkingButton: UIButton { override func backgroundRect(forBounds bounds: CGRect) -> CGRect { var result = super.backgroundRect(forBounds:bounds) if self.isHighlighted { result = result.insetBy(dx: 3, dy: 3) } return result } override var intrinsicContentSize : CGSize { return super.intrinsicContentSize.sizeByDelta(dw:25, dh: 20) } }

The effect is subtle but lively and clear. Here is the button in its normal state and in its highlighted state.

<a href="https://i.stack.imgur.com/CnkYA.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/CnkYA.png" data-original="https://i.stack.imgur.com/CnkYA.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a> <a href="https://i.stack.imgur.com/5nhE8.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/5nhE8.png" data-original="https://i.stack.imgur.com/5nhE8.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>


By default, backgroundRect(forBounds:) returns the <strong>same value</strong> as bounds. However, you can override it in a subclass to prevent drawing over your custom content.

From <a href="https://developer.apple.com/documentation/uikit/uibutton/1624015-backgroundrect" rel="nofollow">Apple Developer Documentation</a>:


The default implementation of this method returns the value in the bounds parameter. This rectangle represents the area in which the button draws its standard background content. Subclasses that provide custom background adornments can override this method and return a modified bounds rectangle to prevent the button from drawing over any custom content.


contentRect(forBounds:) returns the area that is used for displaying the title and the image of the button.

titleRect(forContentRect:) and imageRect(forContentRect:) return the rectangle of the title/image.

Here is some visual help for the return value of all four of these methods for a non-overridden, standard button:

<a href="https://i.stack.imgur.com/nxDun.png" rel="nofollow"><img alt="visual representation of the return value of the methods mentioned above" class="b-lazy" data-src="https://i.stack.imgur.com/nxDun.png" data-original="https://i.stack.imgur.com/nxDun.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>


  • Sources for an Android Image gallery view with pinch zoom, touch swipe etc
  • Move two paddles at same time in SpriteKit + Swift
  • Creating HyperLink in Notepad(textEdit)[MFC]
  • Creating HyperLink in Notepad(textEdit)[MFC]
  • Android onInterceptTouchEvent, get childs in ViewGroup
  • Horizontal ScrollView full Scroll focus right with ViewTreeObserver
  • I processed fingerprints. How do I compare the resuts?
  • SKPhysicsJointFixed doesn't keep nodes together while moving one
  • Take snapshot / screenshot of whole UIView - iPhone
  • basic.Nack not being processed
  • Swift: Make an image in background of label
  • Simulating drags inside a scrollview
  • What's wrong with this mysql create trigger statement
  • moving an object without actually touching the object
  • jQuery.on “touchstart mousedown” - both events are triggered on touchscreen device
  • return common fields in two files
  • Is it possible to put layout xml's in subdirectories under /res/layout?
  • Grow Animation for Floating Action Button
  • FParsec: how to combine parsers so that they will be matched in arbitrary order
  • Aspect ratio behaviour of - internal dimensions and element size
  • UILabel extra spaces before and after text ios
  • Level-order tree traversal
  • VB.NET 2012 Property Set on Property Get
  • init_seg and warning C4073 from library code?
  • How to know which Linq statement produced the SQL on hand during runtime?
  • Easiest way to get current unix timestamp via XSL
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • How to get current document uri in XSLT?
  • How does this usort cmp function actually work?
  • What and where is mdimport
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Functions in global context
  • Recording logins for password protected directories
  • Update CALayer sublayers immediately
  • Pass value from viewmodel to script in zk
  • Android screen density dpi vs ppi
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Change an a tag attribute in JavaScript based on screen width
  • Benchmarking RAM performance - UWP and C#
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass