iOS - 将视图锚定到屏幕顶部,状态栏后面

iOS - anchor view to the top of the screen, behind status bar

我有一个带有默认状态栏的 CollectionViewController,它全部嵌入到 NavigationController 中。

我正在尝试重现一种效果,在该效果中,我将一个视图锚定在屏幕顶部,因为我的 navController 是完全透明的,我的状态栏也是透明的,但是不知何故,当我将我的视图锚定到topAnchor 它保持与 NavigationController bottomAnchor 的剪辑,因为它 'thinks' 屏幕的顶部是 navController 的底部。

在将视图锚定到顶部后,我设法绕过了它,我给它一个负的 topPadding,它现在是我想要的位置。

我知道有一种方法可以将布局扩展到顶部屏幕,但我想不通。

你能给我一个提示吗?

我正在尝试这个:

我明白了:

白色的space是状态栏高度+NavigationController高度

谢谢

如果您不希望 ViewControllers 内容视图在 top/bottom 栏下延伸,您可以使用 Interface Builder 中的“在顶部 Bars/Under 底部栏下延伸边缘”设置。

试试这个

目标 C:

self.edgesForExtendedLayout = UIRectEdgeNone;

Swift3:

self.edgesForExtendedLayout = []

希望对您有所帮助:)

好吧,这会将我的 UIImageView 固定到屏幕顶部...在导航栏和状态栏下方。与集合视图应该没有什么不同:

NSLayoutConstraint *top = [NSLayoutConstraint
                           constraintWithItem:imageView
                           attribute:NSLayoutAttributeTop
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.view
                           attribute:NSLayoutAttributeTop
                           multiplier:1
                           constant:0];

编辑:抱歉,我正在做一个 Obj-C 项目...这里是 Swift:

    let img = UIImage(named: "Man")
    let imageView = UIImageView(image: img)
    imageView.translatesAutoresizingMaskIntoConstraints = false

    self.view.addSubview(imageView)

    imageView.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
    imageView.heightAnchor.constraint(equalToConstant: 200.0).isActive = true

    imageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

    imageView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true

并且...这里还有一个集合视图(很难用普通的屏幕截图来分辨):

    self.view.backgroundColor = .white

    let img = UIImage(named: "Man")
    let imageView = UIImageView(image: img)
    imageView.translatesAutoresizingMaskIntoConstraints = false

    self.view.addSubview(imageView)

    imageView.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
    imageView.heightAnchor.constraint(equalToConstant: 200.0).isActive = true

    imageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

    imageView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true

    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    cv.backgroundColor = .clear

    cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "defaultCell")
    cv.dataSource = self

    cv.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(cv)

    cv.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.9).isActive = true
    cv.heightAnchor.constraint(equalToConstant: 300.0).isActive = true

    cv.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

    cv.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true

class DetailsPage : UIViewController {

    let topView:UIView = UIView()
    let topSliderImage:UIImageView = UIImageView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        setUI()
    }

    override func viewWillAppear(_ animated: Bool) { 
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage() 
    }

    func setUI(){

        self.view.addSubview(topSliderImage)
        topSliderImage.image = UIImage(named: "img-1")
        topSliderImage.contentMode = .scaleAspectFit
        topSliderImage.translatesAutoresizingMaskIntoConstraints = false
        topSliderImage.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
        topSliderImage.heightAnchor.constraint(equalToConstant: 300).isActive = true
        topSliderImage.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
    }
}