2012-02-26 27 views
5

是否有可能在iOS上視圖始終浮動超過所有其他視圖。我問這是因爲我想實現的是一個浮動在ViewController上方的視圖,然後是一個Modal View Controller滑入,而該特定視圖仍然懸浮在該Modal View Controller上(希望你能得到我想說的內容)。視圖浮動高於所有視圖控制器

回答

8

有。您可以將您的視圖添加到主要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; 
} 
+0

好的,當你使用故事板時,這個功能是否也會起作用? – thvanarkel 2012-02-26 14:29:25

+0

我相信它應該,但我沒有嘗試過。有可能你必須製作一個方法來將_anotherViewToFront和viewControllers更改位置後調用它。 – 2012-02-26 14:38:12

3

你可以,如果你使用的是故事板和自動佈局(由第一個答案的啓發)

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