在所有的视图控制器之上显示浮动视图

8

在iOS上,是否有可能使一个视图始终浮动在所有其他视图之上。我之所以问这个问题,是因为我想实现的是一个视图浮动在ViewController之上,然后一个Modal View Controller滑入,而该特定视图仍浮动在该Modal View Controller之上(希望你明白我的意思)。

3个回答

9

有的。当需要时,您可以将您的视图添加到主要的window并将其置于前台。

在下面的代码中,假设_viewConroller_anotherView是appDelegate的强属性 - 当然,配置可能不同。

以下代码将在屏幕左上角添加一个小蓝色正方形。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    _viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    _anotherView = [[UIView alloc] initWithFrame: CGRectMake (0.0,0.0,20.0,20.0)];
    [anotherView setBackgroundColor: [UIColor blueColor]];    

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    [self.window addSubView: _anotherView];
    [self.window bringSubViewToFront: _anotherView]; //not really needed here but it doesn't do any harm

    return YES;
}

我相信它应该可以,但我还没有尝试过。你可能需要创建一个方法将_anotherViewToFront带到前面,在viewControllers交换位置之后调用它。 - Rok Jarc

3
如果您正在使用Storyboard和AutoLayout,可以执行以下操作(受第一个答案的启发):
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *_vc = [sb instantiateViewControllerWithIdentifier:@"FloatingController"];

_anotherView = _vc.view;
[_anotherView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.window addSubview: _anotherView];

[[VLBConstraintsGenerator sharedInstance] setWidth:200 forView:_anotherView inSuperView:_window];
[[VLBConstraintsGenerator sharedInstance] setHeight:200 forView:_anotherView inSuperView:_window];
[[VLBConstraintsGenerator sharedInstance] setLeading:0 forView:_anotherView inSuperView:_window];


[_anotherView setBackgroundColor:[UIColor grayColor]];

[[_anotherView layer] setBorderWidth:1];
[[_anotherView layer] setBorderColor:[UIColor yellowColor].CGColor];

[self.window makeKeyAndVisible];
[self.window bringSubviewToFront:_anotherView]; //not really needed here but it doesn't do any harm

您需要做的是将一个视图控制器拖入您的主故事板,并将其设为FloatingController作为故事板ID。

附加方法

-(void)setWidth:(CGFloat )theWidth forView:(UIView *)theView inSuperView:(UIView *)theSuperView

{
 assert([theSuperView isEqual:theView.superview]);
    NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:theView
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1
                                                           constant:theWidth];


//    [cn setPriority:999];//make it variable according to the orientation
[theSuperView addConstraint:cn];
}


-(void)setHeight:(CGFloat )theHeight forView:(UIView *)theView inSuperView:(UIView *)theSuperView
{
assert([theSuperView isEqual:theView.superview]);

NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:theView
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute:NSLayoutAttributeNotAnAttribute
                                                     multiplier:1
                                                       constant:theHeight];

[theSuperView addConstraint:cn];
}

-(void)setLeading:(CGFloat )theLeading forView:(UIView *)theView inSuperView:(UIView *)theSuperView
{
assert([theSuperView isEqual:theView.superview]);

NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:theView
                                                      attribute:NSLayoutAttributeLeading
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:theSuperView
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1
                                                       constant:theLeading];

[theSuperView addConstraint:cn];
}

1
我的解决方案大致如下。我将创建一个UIView,里面包含UILabel。
首先在某个类的内部设置如下:
 private var bannerLabel: UILabel = UILabel()
    private let view = UIView()

    // label background color 
    let greenColor = UIColorFromRGB(63,g: 161,b: 81)
    let warningColor = UIColorFromRGB(252,g: 140,b: 38)
    let errorColor = UIColorFromRGB(252,g: 66,b: 54)

之后,您可以添加类似以下内容的代码。
func showBannerWithOption(title: String, color: UIColor) {
        view.frame = CGRect(x: 50, y: 150, width: 200, height: 45)
        let window = UIApplication.shared.keyWindow
        guard let mainWindow = window else { return }
        mainWindow.addSubview(view)

        view.centerXAnchor.constraint(equalTo: mainWindow.centerXAnchor).isActive = true
        view.centerYAnchor.constraint(equalTo: mainWindow.centerYAnchor).isActive = true

        bannerLabel.frame = CGRect(x: 50, y: 150, width: 200, height: 21)
        bannerLabel.backgroundColor = color
        bannerLabel.textColor = UIColor.black
        bannerLabel.textAlignment = .center
        bannerLabel.text = title
        view.addSubview(bannerLabel)
        bannerLabel.translatesAutoresizingMaskIntoConstraints = false

        bannerLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
        bannerLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
        bannerLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
        bannerLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
    }

首先,我添加一个视图并设置一些CGRect,然后我设置了一个窗口(主窗口),在里面添加了一个创建的视图。

之后,我设置了一个标签,并设置了与视图相等的约束。

如果您需要在多个地方使用标题和颜色显示横幅,可以使用以下函数:function showBannerWithOption。

如果您想调用此函数,只需执行以下操作:

    override func viewDidLoad() {
        super.viewDidLoad()
    showBannerWithOption(title: "test", color: greenColor) 
}

这就是全部内容 :) 这是Swift 5.0

现在你可以根据自己的需要进行定制。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接