视图浮动在所有ViewControllers之上

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

有。 您可以将视图添加到主window并在必要时将其放在前面。

在下面的代码中,推测_viewConroller_anotherView_anotherView的强大属性 – configuration当然可以不同。

这段代码会在屏幕的左上angular添加一个小蓝色方块。

 - (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; } 

如果您正在使用故事板和自动布局(受第一个答案的启发),则可以执行以下操作:

 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]; }