Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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应用程序的导航栏中对齐项目_Ios_Swift_Layout_Uinavigationbar_Frame - Fatal编程技术网

在iOS应用程序的导航栏中对齐项目

在iOS应用程序的导航栏中对齐项目,ios,swift,layout,uinavigationbar,frame,Ios,Swift,Layout,Uinavigationbar,Frame,我有一个导航栏,其中包含一个自定义视图,其中有一个名称和照片。此customview的布局包含使用定位对齐的子视图。然后我尝试添加一个我想显示在导航栏右侧的按钮,所以我这样添加了它,我之所以这样添加而不是传统方式,是因为我的图像被拉伸了,如果我使用它作为传统意义上的背景图像: //Adding Unmatch Button let unmatchitem = UIBarButtonItem(image: UIImage(named: "heartbreak")?.

我有一个导航栏,其中包含一个自定义视图,其中有一个名称和照片。此customview的布局包含使用定位对齐的子视图。然后我尝试添加一个我想显示在导航栏右侧的按钮,所以我这样添加了它,我之所以这样添加而不是传统方式,是因为我的图像被拉伸了,如果我使用它作为传统意义上的背景图像:

        //Adding Unmatch Button
        let unmatchitem = UIBarButtonItem(image: UIImage(named: "heartbreak")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(unmatchTapped))
        navigationItem.rightBarButtonItem = unmatchitem

但它在我的应用程序中显示如下:

正如你所看到的,右边的按钮位于中间。这是不应该的。我创建customview的方式是使用锚定,下面是我使用的一些锚定:

图像锚定

        profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
        profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
名称标签和标题视图的锚点,其中包含保存标签和图像的containerview

        nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
        nameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
        nameLabel.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true

        containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
        containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true

我不确定我能做些什么使这个正确的按钮实际上在右边。因为在我添加右按钮之前,我的customview完全居中,所以我不认为我需要更改约束,而这与我的自定义按钮有关,但是我找不到锚属性,因此我不确定如何将其对齐到右侧。任何帮助都将不胜感激

编辑: 我相信解决方案是将按钮添加到我创建的自定义视图中。它正在使用以下代码开始工作:

        //Adding Unmatch Button to titleview
        let unmatchButton = UIButton()
        unmatchButton.setBackgroundImage(UIImage(named: "heartbreak")?.withRenderingMode(.alwaysOriginal), for: .normal)
        titleView.addSubview(unmatchButton)
        unmatchButton.translatesAutoresizingMaskIntoConstraints = false
        //create size for the button
        unmatchButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
        unmatchButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
//        Unmatch Button Constraints
        unmatchButton.leftAnchor.constraint(equalTo: containerView.rightAnchor, constant: 32).isActive = true
//        unmatchButton.leftAnchor.constraint(equalTo: nameLabel.rightAnchor).isActive = true
        unmatchButton.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true

        self.navigationItem.titleView = titleView
我遇到的一个问题是,如何将其锚定到导航栏的右侧。因为如果我把它锚定在名字的左边,它就不起作用了,因为这个容器是根据名字的长度拉伸的,所以我希望它每次锚定在导航栏的右边,都能永久地在同一个位置,但我不知道我能锚定到什么位置。我试过这个:

        unmatchButton.rightAnchor.constraint(equalTo: (navigationController?.navigationBar.rightAnchor)!).isActive = true

但是我得到一个错误,它不能这样做,因为它们没有锚祖先。

所以您有一个
navigationItem.titleView
作为自定义
UIView
?尝试使用调试视图层次结构更好地了解元素的布局方式!我想你是在暗示我应该将它添加到我的customview本身,而不是作为一个rightbarbutton。我会试试:)所以你有一个
navigationItem.titleView
作为自定义
UIView
?尝试使用调试视图层次结构更好地了解元素的布局方式!我想你是在暗示我应该将它添加到我的customview本身,而不是作为一个rightbarbutton。我试试看:)