【问题标题】:How to add subview with half-transparent background which won't break and won't be affected by AutoLayout?如何添加不会破坏且不受AutoLayout影响的半透明背景的子视图?
【发布时间】:2017-06-18 09:01:04
【问题描述】:

我想向用户展示应用程序正在使用一些进度条和视图顶部的标签来加载数据,这将在加载数据后显示。此视图不是初始视图,因此我无法将 LaunchScreen 用于这些目的。最好的方法是什么?

后面要显示的视图使用AutoLayout,最好是最上面的视图也使用AutoLayout,但是这些AutoLayout之间不应该有任何交互。

【问题讨论】:

    标签: ios objective-c ios-autolayout


    【解决方案1】:

    首先创建一个自定义的 UIView 类,并根据您的要求自定义视图。即透明背景,使用自动布局/在代码的帮助下添加图像/标签等。

    现在将这个自定义视图 (yourSubView) 添加到它的容器视图中并添加一些约束,使其放置在正确的位置。

    以下代码将在所有边(左、右、上、下)保持80px空间后添加一个子视图,您可以根据需要添加约束。

        [yourContainerView addSubview:yourSubView];
        yourSubView.translatesAutoresizingMaskIntoConstraints =
        NO;
    
        [yourContainerView addConstraint:[NSLayoutConstraint constraintWithItem:yourSubView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:yourContainerView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:80.0]];
        [yourContainerView addConstraint:[NSLayoutConstraint constraintWithItem:yourSubView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:yourContainerView attribute:NSLayoutAttributeRight multiplier:1.0 constant:80.0]];
        [yourContainerView addConstraint:[NSLayoutConstraint constraintWithItem:yourSubView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:yourContainerView attribute:NSLayoutAttributeTop multiplier:1.0 constant:80.0]];
        [yourContainerView addConstraint:[NSLayoutConstraint constraintWithItem:yourSubView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:yourContainerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:80.0]];
    

    【讨论】:

      【解决方案2】:

      使用https://github.com/sgryschuk/SGNavigationProgress 之类的东西怎么样?

      【讨论】:

        猜你喜欢
        • 2015-05-24
        • 2011-09-15
        • 2013-07-03
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-12
        • 1970-01-01
        相关资源
        最近更新 更多