Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios layoutMarginsGuide生成不需要的前导和尾随边距_Ios_Swift_Layout_Autolayout - Fatal编程技术网

Ios layoutMarginsGuide生成不需要的前导和尾随边距

Ios layoutMarginsGuide生成不需要的前导和尾随边距,ios,swift,layout,autolayout,Ios,Swift,Layout,Autolayout,我正在以编程方式在视图上创建约束。当我尝试使用layoutMarginsGuide锚定时,顶部和底部锚定按预期工作,但前导锚定和尾随锚定会创建边距,即使插入设置为0。是什么造成了这些不需要的边距?如何正确设置这些边距 override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemRed view.directionalLayoutMargins

我正在以编程方式在视图上创建约束。当我尝试使用
layoutMarginsGuide
锚定时,顶部和底部锚定按预期工作,但前导锚定和尾随锚定会创建边距,即使插入设置为0。是什么造成了这些不需要的边距?如何正确设置这些边距

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemRed
        view.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)


        let childView = UIView(frame: .zero)
        childView.backgroundColor = .systemIndigo
        childView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(childView)
        NSLayoutConstraint.activate([
            childView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
            childView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),
            childView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
            childView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
        ])
}

为了使侧面约束不包含空格并完全粘在侧面上,您必须删除layoutMarginsGuide,如下所示:

NSLayoutConstraint.activate([
      childView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
      childView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),
      childView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      childView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])

为了使侧面约束不包含空格并完全粘在侧面上,您必须删除layoutMarginsGuide,如下所示:

NSLayoutConstraint.activate([
      childView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
      childView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),
      childView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
      childView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])

与其他视图不同,系统管理视图控制器根视图的边距。默认情况下,它根据视图宽度强制执行16点或20点的最小左右边距。默认情况下,上下页边距为零

所以,如果您希望根视图的边距更小,则系统最小。您必须将设为false查看相关系统最小布局边距

viewRespectsSystemMinimumLayoutMargins = false
view.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)

与其他视图不同,系统管理视图控制器根视图的边距。默认情况下,它根据视图宽度强制执行16点或20点的最小左右边距。默认情况下,上下页边距为零

所以,如果您希望根视图的边距更小,则系统最小。您必须将设为false查看相关系统最小布局边距

viewRespectsSystemMinimumLayoutMargins = false
view.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)

只需尝试通过以下方式打印systemMinimumLayoutmargins:print(systemMinimumLayoutmargins)只需尝试通过以下方式打印systemMinimumLayoutmargins:print(systemMinimumLayoutmargins)