Safe Area of Xcode 9

后端 未结 5 422
长情又很酷
长情又很酷 2020-11-22 10:55

While exploring Xcode9 Beta Found Safe Area on Interface builders View hierarchy viewer. Got curious and tried to know about Safe Area on A

5条回答
  •  名媛妹妹
    2020-11-22 11:50

    • Earlier in iOS 7.0–11.0 <Deprecated> UIKit uses the topLayoutGuide & bottomLayoutGuide which is UIView property
    • iOS11+ uses safeAreaLayoutGuide which is also UIView property

    • Enable Safe Area Layout Guide check box from file inspector.

    • Safe areas help you place your views within the visible portion of the overall interface.

    • In tvOS, the safe area also includes the screen’s overscan insets, which represent the area covered by the screen’s bezel.

    • safeAreaLayoutGuide reflects the portion of the view that is not covered by navigation bars, tab bars, toolbars, and other ancestor viewss.
    • Use safe areas as an aid to laying out your content like UIButton etc.

    • When designing for iPhone X, you must ensure that layouts fill the screen and aren't obscured by the device's rounded corners, sensor housing, or the indicator for accessing the Home screen.

    • Make sure backgrounds extend to the edges of the display, and that vertically scrollable layouts, like tables and collections, continue all the way to the bottom.

    • The status bar is taller on iPhone X than on other iPhones. If your app assumes a fixed status bar height for positioning content below the status bar, you must update your app to dynamically position content based on the user's device. Note that the status bar on iPhone X doesn't change height when background tasks like voice recording and location tracking are active print(UIApplication.shared.statusBarFrame.height)//44 for iPhone X, 20 for other iPhones

    • Height of home indicator container is 34 points.

    • Once you enable Safe Area Layout Guide you can see safe area constraints property listed in the interface builder.

    You can set constraints with respective of self.view.safeAreaLayoutGuide as-

    ObjC:

      self.demoView.translatesAutoresizingMaskIntoConstraints = NO;
        UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
        [self.demoView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
        [self.demoView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
        [self.demoView.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
        [self.demoView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
    

    Swift:

       demoView.translatesAutoresizingMaskIntoConstraints = false
            if #available(iOS 11.0, *) {
                let guide = self.view.safeAreaLayoutGuide
                demoView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
                demoView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
                demoView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
                demoView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
            } else {
                NSLayoutConstraint(item: demoView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
                NSLayoutConstraint(item: demoView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true
                NSLayoutConstraint(item: demoView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
                NSLayoutConstraint(item: demoView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
            }
    

提交回复
热议问题