2012-02-26 13 views
5

E 'possibile su iOS che una vista fluttui sempre sopra tutte le altre viste. Lo chiedo perché quello che mi piacerebbe ottenere è una vista che fluttua sopra un ViewController, e poi un controller di modal view scorre, mentre quella particolare vista è ancora fluttuante su quel Modal View Controller (spero che tu ottenga quello che sto cercando di dire).Visualizza mobile sopra tutti ViewControllers

risposta

8

C'è. È possibile aggiungere la vista allo window principale e portarlo in primo piano quando è necessario.

Nel seguente codice si presume che _viewConroller e _anotherView siano proprietà forti di appDelegate - la configurazione potrebbe ovviamente essere diversa.

Questo codice aggiungerebbe un piccolo quadrato blu nell'angolo in alto a sinistra dello schermo.

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

Ok, funzionerà anche quando si utilizza lo storyboard? – thvanarkel

+0

Credo che dovrebbe, ma non l'ho provato. È possibile che tu debba fare un metodo per portare _anotherViewToFront e chiamarlo dopo che viewControllers cambia posizione. –

3

È possibile eseguire le seguenti operazioni se si utilizza storyboard e layout automatico (ispirata alla prima risposta)

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 

tutto quello che dovete fare è di trascinare un controller della vista in voi storyboard principale con FloatingController come ID storyboard

metodi aggiuntivo

-(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]; 
} 
Problemi correlati