目标C:如何以编程方式在Safe Area中创建self.view [英] Objective C : How to create self.view inside Safe Area programmatically

查看:172
本文介绍了目标C:如何以编程方式在Safe Area中创建self.view的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的应用从支持iOS 8改为支持iOS 9及更高版本。



由于我没有使用故事板来创建我的观点,我想知道是否有编程方式的使用安全区指南选项像那样。



我试图锚定我的观点,但他们不断重叠顶部&在iPhone X模拟器的底部。

解决方案

在Objective-C中试一试,看看:

  UIView * myView = //使用IBOutlet初始化视图或编程

myView.backgroundColor = [UIColor redColor];
myView.translatesAutoresizingMaskIntoConstraints = NO;

if(@available(iOS 11,*)){
UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
[myView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor] .active = YES;
[myView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor] .active = YES;
[myView.topAnchor constraintEqualToAnchor:guide.topAnchor] .active = YES;
[myView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor] .active = YES;
}其他{
UILayoutGuide * margins = self.view.layoutMarginsGuide;
[myView.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor] .active = YES;
[myView.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor] .active = YES;
[myView.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor] .active = YES;
[myView.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor] .active = YES;

}

//刷新myView和/或主视图
[self.view layoutIfNeeded];
// [self.myView layoutIfNeeded];

参考:


I have just changed my app from supporting iOS 8 and up to supporting iOS 9 and up.

Since I don't use storyboards to create my views, I was wondering if there's the "Use Safe Area Guides" option programmatically or something like that.

I've tried to anchor my view but they keep overlapping the top & bottom in the iPhone X simulator.

解决方案

Try this in Objective-C and see:

UIView * myView = // initialize view using IBOutlet or programtically

myView.backgroundColor = [UIColor redColor];
myView.translatesAutoresizingMaskIntoConstraints = NO;

if (@available(iOS 11, *)) {
    UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
    [myView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
    [myView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
    [myView.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
    [myView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
} else {
    UILayoutGuide *margins = self.view.layoutMarginsGuide;
    [myView.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
    [myView.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
    [myView.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES;
    [myView.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor].active = YES;

}

// Refresh myView and/or main view
[self.view layoutIfNeeded];
//[self.myView layoutIfNeeded];

Ref from: Use Safe Area Layout programmatically - Swift

Result:

这篇关于目标C:如何以编程方式在Safe Area中创建self.view的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆