代码之家  ›  专栏  ›  技术社区  ›  ijason03

iView高度不适用于iPhone X

  •  1
  • ijason03  · 技术社区  · 7 年前


    我尝试了几种方法来扩展视图以填充屏幕,但似乎没有任何效果。我将视图高度设置为Aspect Fill,将内容模式设置为Aspect Fill,将Scale设置为Fill with no luck。

    [self.view setBackgroundColor:[UIColor yellowColor]];
    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    

    我尝试将视图的框架设置为主屏幕的边界。那没用。

    CGRect frame = [[UIScreen mainScreen] bounds];
    self.view.frame = frame;
    

    我尝试为主视图设置高度约束,但没有成功。但我不确定我是否做对了。

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view
                                                                  attribute:NSLayoutAttributeHeight
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:nil
                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                 multiplier:1
                                                                   constant:812]];
    

    谁能告诉我我做错了什么?

    UIView not full screen

    2 回复  |  直到 7 年前
        1
  •  2
  •   Arun Balakrishnan    7 年前

    做这些检查

    1. rootViewController 的帧设置为 [[UIScreen mainScreen] bounds] 在里面 didFinishLaunchingWithOptions 属于 AppDelegate ? 像这样的

      自己窗rootViewController。看法帧=[[UIScreen mainScreen]边界];

    2. 检查是否 UIWindow 的帧已设置为 mainScreen 界限。

      self.window.frame = [[UIScreen mainScreen] bounds]

    3. 通过启用,检查是否打破了任何自动布局约束 约束错误断点

        2
  •  1
  •   Magoo    7 年前

    toItem:nil 应该是要将其约束到的视图。。。例如 superview .... 这 viewConstraint 将此视图的高度设置为 812.0

    你需要改变 NSLayoutAttributeNotAnAttribute NSLayoutAttributeHeight toItem self.view.superview 或者随便什么 view 你想把它绑定到。。。和改变 812 0.0

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view
                                                              attribute:NSLayoutAttributeHeight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.view.superview
                                                              attribute:NSLayoutAttributeHeight
                                                             multiplier:1.0
                                                               constant:0.0]];
    

    不过我会问。。。是 self.view 这个 viewControllers 看法 ? 你不需要设置 frame 无论如何。。。除非 viewController 它应该自动将整个屏幕视为 bounds