iOS7 - View under status bar - edgesForExtendedLayout not working

前端 未结 7 1884
予麋鹿
予麋鹿 2020-12-28 17:30

I have a project that was built last year, and it uses XIBs, no storyboards. The XIBs do not use Auto Layout, but they do use some Autosizing. I have an issue when running

相关标签:
7条回答
  • 2020-12-28 17:52
    - (void)viewDidAppear:(BOOL)animated {
            [self.view setFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
    // OR
            self.view.transform = CGAffineTransformMakeTranslation(0, 20);
    }
    
    0 讨论(0)
  • 2020-12-28 17:55

    If you are using the storyboard, after setting the top layout of your view, you can uncheck the "Under Opaque Bars" in the Attributes Inspector

    Storyboard

    0 讨论(0)
  • 2020-12-28 17:56

    Have you tried viewing your XIBs as source and removing any line containing edgesforextendedlayout ??

    We had to remove this line in our storyboard's scenes since our storyboard's scenes' main views are represented by XIBs

    What was happening for us was that somehow, in some scenes, the XIB content for the scene's main view was being pushed down by the height of the status bar and the navigation bar.

    Removing that line allowed the XIBs to be displayed as if their top originated at the same top of its storyboard's scene.

    Sadly, we have no idea what triggered this, but I saw it happen when changing the order of the contents within the XIB's main view so that a UITextView appeared first. Rearranging the order of items after this was triggered had no effect in removing this unwanted behaviour.

    Hope this helps anyone else running into this type of problem.

    0 讨论(0)
  • 2020-12-28 17:57

    For reference, the solution below did work when I applied it to my ViewControllers. However, it's not ideal and a bit hacky. If it's the only approach I can take, then so be it, though.

    float systemVersion=[[[UIDevice currentDevice] systemVersion] floatValue];
    if(systemVersion>=7.0f)
    {
        CGRect tempRect;
        for(UIView *sub in [[self view] subviews])
        {
            tempRect = [sub frame];
            tempRect.origin.y += 20.0f; //Height of status bar
            [sub setFrame:tempRect];
        }
    }
    
    0 讨论(0)
  • 2020-12-28 17:59

    1)The simplest solution if you don't mind having an opaque navigation bar:

    self.navigationController.navigationBar.translucent = NO;
    

    2) svguerin3's answer can't work in the general case. For example, if one of your subviews uses autosizing to be hooked at the bottom of its container, then its new position will be wrong. And it could go out of screen in the worst case.

    0 讨论(0)
  • 2020-12-28 18:04

    If you set the iOS 6/7 delta values in Interface Builder, remember to set "View as" to "iOS 6" on the Interface Builder Document, since it is the iOS 6 layout you want to replicate. The deltas will then be used only on iOS 7 to push the content below the status bar. If you leave "View as" set to iOS 7 (the default) the deltas will instead give you the iOS 7 look on iOS 6.

    However, the deltas will not help you if you reposition or resize views programmatically based on the view frame, since the frame does not account for the deltas.

    Instead of using the deltas, the best solution I have found is to enable Auto Layout on your main XIB and then set the top space constraint on your top/content view to follow the Top Layout Guide. This guide was introduced in iOS 7 and represents the position below the status bar. Unfortunately the guide is not available in Interface Builder when not using Storyboards, but you can add it programmatically.

    What I did was add a top space constraint to the superview instead in Interface Builder, and created an outlet for this in the code. Then, in viewDidLoad, if the topLayoutGuide is available (iOS 7+), replace the constraint in this outlet with a version using the Top Layout Guide instead.

    if ([self respondsToSelector:@selector(topLayoutGuide)]) {
        [self.view removeConstraint:self.containerTopSpaceConstraint];
    
        self.containerTopSpaceConstraint =
        [NSLayoutConstraint constraintWithItem:self.contentView
                                     attribute:NSLayoutAttributeTop
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.topLayoutGuide
                                     attribute:NSLayoutAttributeBottom
                                    multiplier:1
                                      constant:0];
    
        [self.view addConstraint:self.containerTopSpaceConstraint];
    
        [self.view setNeedsUpdateConstraints];
        [self.view layoutIfNeeded];
    }
    
    0 讨论(0)
提交回复
热议问题