2012-02-26 4 views
5

Возможно ли на iOS, что представление всегда плавает над всеми другими видами. Я спрашиваю об этом, потому что то, что я хотел бы достичь, - это представление, которое плавает над ViewController, а затем контроллер Modal View Control, в то время как этот конкретный вид все еще плавает над этим Modal View Controller (надеюсь, что вы получите то, что я пытаюсь сказать).View over over ViewControllers

ответ

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

+0

Я верю, что это нужно, но я не пробовал. Возможно, вам придётся сделать метод для переноса _anotherViewToFront и вызвать его после того, как viewControllers изменят места. –

3

Вы можете сделать следующее, если вы используете раскадровку и 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]; 
}