什么是方法“backgroundRect(forBounds:)"?UIButton 用于什么? [英] What is the method "backgroundRect(forBounds:)" of UIButton used for?

查看:18
本文介绍了什么是方法“backgroundRect(forBounds:)"?UIButton 用于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 UIButton 有一个 backgroundRect(forBounds:) 函数,但我不明白在哪里以及如何使用它.

我也无法理解如何使用 contentRect(forBounds:)titleRect(forContentRect:)imageRect(forContentRect:)方法.请解释一下.

解决方案

默认情况下,backgroundRect(forBounds:) 返回与 bounds 相同的值代码>.但是,您可以在子类中覆盖它以防止覆盖您的自定义内容.

来自

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.

解决方案

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

From Apple Developer Documentation:

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:

这篇关于什么是方法“backgroundRect(forBounds:)"?UIButton 用于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆