Вид, плавающий над всеми ViewControllers - PullRequest
6 голосов
/ 26 февраля 2012

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

Ответы [ 2 ]

8 голосов
/ 26 февраля 2012

Есть. Вы можете добавить свой вид к основному 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;
}
3 голосов
/ 23 декабря 2013

Вы можете сделать следующее, если вы используете раскадровку и автоматическую разметку (вдохновленный первым ответом)

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 в качестве идентификатора раскадровки

Дополнительные методы

-(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];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...