简体   繁体   English

这些设置在 Interface Builder 中有什么作用?

[英]What do these settings do in Interface Builder?

有问题的设置

This is for a UIButton specifically.这是专门针对UIButton I understand that the alignment options pertain to the label inside the UIButton , but what does the 'content' settings do?我知道对齐选项与UIButton内的标签有关,但“内容”设置有什么作用? They seem like they correspond to the setEnabled: , setSelected: , setHighlighted: methods of the UIButton class, however clicking Highlighted or Selected doesn't seem to change the button's behavior outside of interface builder.它们似乎对应于UIButton类的setEnabled:setSelected:setHighlighted:方法,但是单击 Highlighted 或 Selected 似乎不会更改界面构建器之外的按钮行为。 Also, what would be the use-case for having a button always show highlighted or selected?另外,让按钮始终显示突出显示或选中的用例是什么?

There are several practical uses for these methods.这些方法有多种实际用途。 As @MHUMobileInc.作为@MHUMobileInc。 pointed out, it can be used for a game like Minesweeper.指出,它可以用于像扫雷这样的游戏。 It can also allow you to use the button as a sort of switch, where the selected button has "ON" and the non-selected version has "OFF".它还可以让您将按钮用作一种开关,其中选定的按钮为“ON”,未选定的版本为“OFF”。 It seems that Apple wants developers to shy away from this, and to use either UISwitches or a custom view. Apple 似乎希望开发人员避开这一点,并使用 UISwitches 或自定义视图。 However, Apple does contradict itself in some of its apps .但是,Apple在其某些应用程序中确实自相矛盾

These settings set the initial state of the UIButton- so if in your viewDidLoad you set it not selected nor highlighted, this Interface Builder setting will not be seen on screen when you run the app.这些设置设置了 UIButton 的初始状态 - 因此,如果在您的viewDidLoad中将其设置为未选中或突出显示,则在运行应用程序时将不会在屏幕上看到此 Interface Builder 设置。 If you want to ensure that these settings are kept, it might be easier to say so in code ( viewDidLoad ) rather than Interface Builder.如果您想确保保留这些设置,在代码 ( viewDidLoad ) 中而不是在 Interface Builder 中这样说可能更容易。

It may be easier not to use these settings too much.不要过多地使用这些设置可能会更容易。 They can lead to confusion for you (the developer), other programmers who are working on the project (if any) and the user.它们可能会导致您(开发人员)、从事该项目的其他程序员(如果有)和用户感到困惑。 Use the built-in UIKit views when it works and create your own subclasses when there is nothing that fits best for the situation.当它工作时使用内置的 UIKit 视图,并在没有最适合这种情况时创建你自己的子类。

The upper portion addresses the alignment of the content within the view.上半部分解决了视图内内容的对齐问题。 For example, if you have a large UIButton with an image that's smaller than the bounds of the button, by default the image will be centered vertically and horizontally inside the button.例如,如果您有一个较大的 UIButton,其图像小于按钮的边界,则默认情况下,图像将在按钮内垂直和水平居中。 However, if you want the image to hug the left/right/top/bottom, you can set the alignment using those options.但是,如果您希望图像紧贴左/右/上/下,您可以使用这些选项设置对齐方式。

All of these controls pertain to the UIControl, not UIButton, as indicated by the header in that screenshot.所有这些控件都属于 UIControl,而不是 UIButton,如该屏幕截图中的标题所示。

Alignment refers to contentHorizontalAlignment and contentVerticalAlignment .对齐是指contentHorizontalAlignmentcontentVerticalAlignment Many controls return a fixed width or fixed height, or both, from sizeThatFits: .许多控件从sizeThatFits:返回固定宽度或固定高度,或两者。 For example a UISwitch object has a fixed size, whereas a UISlider object has a fixed height.例如,一个UISwitch对象有一个固定的大小,而一个UISlider对象有一个固定的高度。 If you assign a frame to a control object that does not correspond to these fixed dimensions, these properties determine where in that frame the control interface should be drawn.如果将框架分配给与这些固定尺寸不对应的控件对象,则这些属性将确定控件界面应在该框架中的何处绘制。 Instances of UIButton do fill their frame, so the UIButton class interprets these properties differently to apply to the button's content, but the properties themselves are not specific to the UIButton class. UIButton实例确实填充了它们的框架,因此UIButton类以不同的UIButton解释这些属性以应用于按钮的内容,但这些属性本身并不特定于UIButton类。

Similarly the selected , highlighted and enabled properties are defined by UIControl not UIButton , not all subclasses of UIControl utilise them.类似地, selectedhighlightedenabled属性由UIControl而非UIButton定义,并非UIControl所有子类都使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM