简体   繁体   English

如何知道当前的大小类以及我的按钮是否显示?

[英]How to know the current size classes and if my button is displayed?

I use size classes in my projet. 我在我的项目中使用了大小等级。

I have a button, which is displayed only for 我有一个按钮,只显示

  • Compact Width and Compact height 紧凑的宽度和紧凑的高度
  • Compact width and Regular height 紧凑的宽度和常规的高度

This button isn't displayed for other size classes. 其他尺寸类不显示此按钮。

On this button I apply a specific effect. 在这个按钮上我应用了一个特定的效果。 But I need to apply this effect ONLY for my 2 sizes classes. 但我只需要为我的2个尺寸类应用此效果。

Something like : 就像是 :

if button is available for the current size classe
    apply the effect

Is it possible to implement ? 有可能实施吗?

To follow up on my now-deleted previous answer and subsequent comment, Apple's docs state that 为了跟进我现在删除的以前的答案和随后的评论, Apple的文档说明了这一点

A runtime object for an uninstalled view is still created. 仍会创建已卸载视图的运行时对象。 However, the view and any related constraints are not added to the view hierarchy and the view has a superview property of nil. 但是,视图和任何相关约束不会添加到视图层次结构中,并且视图的superview属性为nil。 This is different from being hidden. 这与隐藏不同。 A hidden view is in the view hierarchy along as are any related constraints. 隐藏视图与任何相关约束一样位于视图层次结构中。

In other words, the only way to know is to check whether the outlet view's parent view is nil . 换句话说,唯一知道的方法是检查出口视图的父视图是否nil

During ViewWillApper() check the traitCollection of your View Controller. 在ViewWillApper()期间,检查View Controller的traitCollection。

myViewController: UIViewController{

    mybutton = UIButton(frame: myframe)

    func viewWillAppear(){

        if traitCollection.horizontalSizeClass == .Compact {
            applyAwesomeEffect(myButton)
        }
    }
}

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

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