如何在导航栏下方提供图像视图填充?

时间:2018-05-25 17:52:54

标签: swift uiimageview

THIS IS WHAT IT LOOKS LIKE

我希望导航栏和图像之间有空间,但它似乎不起作用 任何帮助将非常感激!这是我的代码:

addSubview(profileImageView)
profileImageView.anchor(top: topAnchor, left: self.leftAnchor, bottom: nil, right: nil, paddingTop: 75, paddingLeft: 95, paddingBottom: 12, paddingRight: 12, width: 180, height: 180)
profileImageView.layer.cornerRadius = 180 / 2
profileImageView.clipsToBounds = true

这是声明的变量:

let profileImageView: UIImageView = {
    let iv = UIImageView()
    iv.backgroundColor = .red
    return iv
}()

2 个答案:

答案 0 :(得分:0)

    let topAnchor = self.topAnchor.constraint(equalTo: self.safeTopAnchor, constant: 8)

    let rightAnchor = self.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0)

    let leftAnchor =  self.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0)

    let heightAnchor = self.heightAnchor.constraint(equalToConstant: 180)

    NSLayoutConstraint.activate([topAnchor!, leftAnchor!, rightAnchor, heightAnchor!])

答案 1 :(得分:0)

您可以使用安全区域指南作为UIView排除的Visibale屏幕指南(Tabbar,NavigationBar..etc)

   self.view.addSubview(profileImageView)

    let safeAreaGuide = self.view.safeAreaLayoutGuide //  Safe Area Guide is guide for Visibale Screen of your UIView exclude(Tabbar,NavigationBar..etc)

    // add bading using `constant`
    NSLayoutConstraint.activate([self.profileImageView.topAnchor.constraint(equalTo: safeAreaGuide.topAnchor, constant: 10),
                                 self.profileImageView.widthAnchor.constraint(equalToConstant: 180),
                                 self.profileImageView.heightAnchor.constraint(equalToConstant: 180),
                                 self.profileImageView.leftAnchor.constraint(equalTo: safeAreaGuide.leftAnchor, constant: 95),
                                 self.profileImageView.rightAnchor.constraint(equalTo: safeAreaGuide.rightAnchor, constant: -12),
                                  self.profileImageView.rightAnchor.constraint(equalTo: safeAreaGuide.rightAnchor, constant: -12)])