0

因此,对于新的 iPhone X,我的应用程序中的某些内容处于错误的位置。在我的应用程序底部,我有一个 accesoryView,它基本上是一个带有文本字段和其他元素的 UIView。我在新的 iPhone X 中看到了一些关于 safeAreaLayoutGuide 的东西,但我现在不知道如何在附件视图中实现。所以我试图在我的应用程序中找到一个代码来实现它,所以 safeArea 不再打扰我了。

这是 inputAccesoryView 的代码

lazy var inputContainerView: UIView = {

    let containerView = UIView()
    containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.backgroundColor = UIColor.white


    containerView.addSubview(self.inputTextField)
    containerView.addSubview(self.swiche)
    containerView.addSubview(self.separatorLineView)
    containerView.addSubview(self.uploadImageView)


    //x,y,w,h
    self.inputTextField.leftAnchor.constraint(equalTo: self.swiche.rightAnchor, constant: 4).isActive = true
    self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    self.inputTextField.rightAnchor.constraint(equalTo: self.uploadImageView.leftAnchor, constant: 4).isActive = true
    self.inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
    self.inputTextField.backgroundColor = UIColor.clear


    //x,y,w,h
    self.swiche.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 4).isActive = true
    self.swiche.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    self.swiche.heightAnchor.constraint(equalToConstant: 30).isActive = true
    self.swiche.widthAnchor.constraint(equalToConstant: 55).isActive = true



    //x,y,w,h
    self.uploadImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    self.uploadImageView.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
    self.uploadImageView.widthAnchor.constraint(equalToConstant: 47).isActive = true
    self.uploadImageView.heightAnchor.constraint(equalToConstant: 47).isActive = true


    //x,y,w,h
    self.separatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
    self.separatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
    self.separatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
    self.separatorLineView.heightAnchor.constraint(equalToConstant: 1).isActive = true


    return containerView

}()




//MARK: AccesoryView
override var inputAccessoryView: UIView? {
    get {

        return inputContainerView

    }
}

谢谢您的帮助!!!

4

3 回答 3

0

好的,将此代码粘贴到lazy var之前

override func didMoveToWindow() {
    super.didMoveToWindow()
    if #available(iOS 11.0, *) {
    if let window = self.window {
        self.bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(window.safeAreaLayoutGuide.bottomAnchor, multiplier: 1.0).isActive = true
     }
   }
}

现在你的视图是safeAreaLayoutGuide ...但是在底部你可以看到tableview,因为没有背景(你的视图是safeAreaLayoutGuide),为了纠正这个问题我建立了一个白色的uiview,在inputTextField中呈现它并设置约束:

let dummyView = UIView()
    dummyView.backgroundColor = .white
    dummyView.translatesAutoresizingMaskIntoConstraints = false

现在设置约束:

inputTextField.addSubview(dummyView)
    dummyView.topAnchor.constraint(equalTo: inputTextField.bottomAnchor).isActive = true
    dummyView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    dummyView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    dummyView.heightAnchor.constraint(equalToConstant: 50).isActive = true

这是黑客,但我认为这是 Xcode 错误......我希望这对你有帮助......

于 2017-10-28T18:23:54.950 回答
0

正如我所想,您需要做的就是safeAreaLayoutGuide在指出约束之前访问类。在您的情况下,您需要更改如下约束:

self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true

进入这样的约束:

self.inputTextField.centerYAnchor.constraint(equalTo: containerView.safeAreaLayoutGuide.centerYAnchor).isActive = true

让我知道事情的后续。

于 2017-10-25T02:02:55.370 回答
0

我希望对于您的代码,您只需为 inputTextField 和其他元素添加此底部约束,并将 containerView CGRect 框架高度设置为 100:

self.inputTextField.bottomAnchor.constraint(equalTo: containerView.safeAreaLayoutGuide.bottomAnchor).isActive = true

我想你可以删除:

self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
于 2017-10-29T11:51:50.043 回答