UIControl -contentHorizo​​ntalAlignment:如何在子类中使用

发布于 2024-09-11 15:20:17 字数 1103 浏览 1 评论 0原文

UIControl 有两个属性,它们会影响控件视图中的内容布局:

contentHorizo​​ntalAlignment

The horizontal alignment of content (text or image) within the receiver.

controlVerticalAlignment

The vertical alignment of content (text or image) within the receiver.

我有一个 UIControl 子类,它管理一组五个子视图;每个子视图都是一个自定义的 UIView 子类(即不是 UILabel 或 UIImageView)。

我想要获得的行为是 UIControl 使用 contentHorizo​​ntalAlignmentcontentVerticalAlignment 属性管理自定义子视图的布局。

根据我的实验,在进行子视图布局时,UIControl 实际上并没有遵循这些属性值中的任何一个,至少当子视图不是标签或图像时。不过,我不能 100% 确定我的子类已正确实现。

我的问题有三个:

  1. UIControl 是否实现了围绕这些属性的任何布局行为?
    • 仅适用于 UILabel 和 UIImageView 子视图吗?
  2. 我是否必须在 UIControl 子类中提供子视图布局的实现?
  3. UIControl 基类中存在的这些属性主要是为了向调用者提供一致的 API 来管理内容吗?

鉴于 contentHorizo​​ntalAlignmentcontentVerticalAlignment 属性的存在,我期望 UIControl 提供某种基本级别的布局行为。

我是不是偏离基地了?我不反对实现我自己的布局行为。如果 UIControl 已经提供了一些我应该使用的布局行为,我只是不想与框架对抗。

谢谢,

比尔

UIControl has two properties that are documented to affect the layout of content in the control's view:

contentHorizontalAlignment

The horizontal alignment of content (text or image) within the receiver.

controlVerticalAlignment

The vertical alignment of content (text or image) within the receiver.

I have a UIControl subclass that manages a set of five subviews; each subview is a custom UIView subclass (i.e. not a UILabel or UIImageView).

The behavior I'd like to get is UIControl managing the layout of my custom subviews using the contentHorizontalAlignment and contentVerticalAlignment properties.

From my experimentation, it does not appear that UIControl actually honors either of these property values when doing subview layout, at least when the subview's are not labels or images. I'm not 100% sure that my subclass is properly implemented, though.

My questions are three-ish:

  1. Does UIControl implement any layout behavior around these properties?
    • Is it only for subviews that are UILabel and UIImageView?
  2. Do I have to provide the implementation for subview layout in my UIControl subclass?
  3. Are these properties present in the UIControl base class mainly to provide a caller's with a consistent API for managing content?

I'm expecting UIControl to provide some kind of base-level layout behavior given the presence of contentHorizontalAlignment and contentVerticalAlignment properties.

Am I off-base? I don't object to implementing my own layout behavior. I just don't want to be fighting the framework if UIControl already provides some layout behavior that I should be using.

Thanks,

Bill

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈场末日恋爱 2024-09-18 15:20:17

只是为了跟进这个...

UIControl 只是提供这些属性作为子类的一致 API。基类不对它们做任何事情。

在 UIControl 子类中,重写 -layoutSubviews 方法并使用属性来指导布局逻辑。

例如

switch (self.contentHorizontalAlignment) {
    case UIControlContentHorizontalAlignmentCenter:
    // layout logic for centering
    break; 
}

Just to follow up on this...

UIControl just provides these properties as a consistent API for subclasses. The base class doesn't do anything with them.

In your UIControl subclass, override the -layoutSubviews method and use the properties to guide your layout logic.

e.g.

switch (self.contentHorizontalAlignment) {
    case UIControlContentHorizontalAlignmentCenter:
    // layout logic for centering
    break; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文