我创建了这个“ filterButton”,并将其设置为在“菜单按钮”的确切位置上创建,“ filterButton”直接进入视图的左上角。

lazy var filterButton: UIButton! = {
    let button = UIButton(frame: CGRect(x: self.menuButton.frame.origin.x, y: self.button.frame.origin.y, width: 45, height: 45))
    button.translatesAutoresizingMaskIntoConstraints = false
    button.backgroundColor = .blue
    button.clipsToBounds = true
    return button
}()


当我检查并打印“ filterButton”的框架时,它具有“ menuButton”的值,为什么会发生这种情况?

最佳答案

在向视图添加filterButton的位置添加以下约束

filterButton.leftAnchor.constraint(equalTo: menuButton.leftAnchor).isActive = true
filterButton.topAnchor.constraint(equalTo: menuButton.topAnchor).isActive = true
filterButton.rightAnchor.constraint(equalTo: menuButton.rightAnchor).isActive = true
filterButton.bottomAnchor.constraint(equalTo: menuButton.bottomAnchor).isActive = true

10-07 17:19