Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 浮动在所有视图控制器上方的视图_Iphone_Ios_Uikit - Fatal编程技术网

Iphone 浮动在所有视图控制器上方的视图

Iphone 浮动在所有视图控制器上方的视图,iphone,ios,uikit,Iphone,Ios,Uikit,在iOS上是否可能一个视图总是浮在所有其他视图之上。我问这个问题是因为我想要实现的是一个浮动在ViewController上的视图,然后一个模态视图控制器滑入,而这个特定视图仍然浮动在模态视图控制器上(希望你明白我想说的)。有。您可以将视图添加到主窗口,并在必要时将其置于最前面 在下面的代码中,假定\u viewConroller和\u其他视图是appDelegate的强属性-配置当然可能不同 这段代码将在屏幕左上角添加一个蓝色的小正方形 - (BOOL)application:(UIAppli

在iOS上是否可能一个视图总是浮在所有其他视图之上。我问这个问题是因为我想要实现的是一个浮动在ViewController上的视图,然后一个模态视图控制器滑入,而这个特定视图仍然浮动在模态视图控制器上(希望你明白我想说的)。

有。您可以将视图添加到主
窗口
,并在必要时将其置于最前面

在下面的代码中,假定
\u viewConroller
\u其他视图
是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;
}

如果您使用故事板和自动布局(灵感来自第一个答案),则可以执行以下操作

您只需将视图控制器拖动到主情节提要中,并将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];
}

我对这个问题的解决办法是这样的。 我将创建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,然后我设置了一个窗口(主窗口),在该窗口内,我添加了一个创建的视图

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

如果您需要在多个位置使用带有标题和颜色的函数showBannerWithOption,则该函数与此类似

如果要调用此函数,请执行以下操作:

    override func viewDidLoad() {
        super.viewDidLoad()
    showBannerWithOption(title: "test", color: greenColor) 
}
仅此而已:) 这是swift 5.0


现在,您可以根据需要进行自定义

我相信应该,但我没有试过。您可能需要创建一个方法,将另一个视图带到前面,并在ViewController更改位置后调用它。
    override func viewDidLoad() {
        super.viewDidLoad()
    showBannerWithOption(title: "test", color: greenColor) 
}