以编程方式限制按钮

问题描述

我有图库,当我点击图片时,我有 objc 功能点击按钮,在这个事件中,我需要全屏图像并到达图片按钮下方。 我需要移动按钮,但它不起作用 我使用框架作为按钮但没有工作,也没有工作按钮点击里面 当我在 func onButton 中编写约束时出现错误->“无法使用锚点激活约束”有什么方法可以在图片下方添加按钮? Button in top

@objc func onButton(sender: UIButton){

    let imageView = sender.currentimage!
    let newImageView = UIImageView(image: imageView)
    let imgFrame = UIScreen.main.bounds
    newImageView.frame = imgFrame
    newImageView.backgroundColor = .white
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self,action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
    newImageView.addSubview(someButton)
}
@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    self.navigationController?.isNavigationBarHidden = false
    self.tabBarController?.tabBar.isHidden = false
    sender.view?.removeFromSuperview()
}

private let someButton: UIButton = {
    let button = UIButton()
    button.frame = CGRect(x: 24,y: 100,width: 100,height: 54)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Make Profile Pic",for: .normal)
    button.setTitleColor(.black,for: .normal)
    button.addTarget(self,action: #selector(onSomeButton(_:)),for: .touchUpInside)
    button.backgroundColor = .purple
    
    return button
}()

解决方法

首先,欢迎使用堆栈溢出:) 其次,您的问题很难理解,请考虑使用文字处理器对您的问题进行拼写/语法检查,然后再将其发布到此处。如果我们了解您的要求,我们可以更快地为您提供帮助哈哈:P

第三。我相信您的问题在于您对 someButton

的声明
button.frame = CGRect(x: 24,y: 100,width: 100,height: 54)
button.translatesAutoresizingMaskIntoConstraints = false

此处的 Y 坐标距离顶部仅 100。

考虑到 iPhone 屏幕尺寸的多样性,我建议在您的代码中采用自动布局。

Xcode 现在对我不太友好,但你应该寻找这样的东西:

var button = UIButton() //declare your button
button.translatesAutoresizingMaskIntoConstraints = false //tell your button to use the constraints you set
view.addSubView(button) // add the button to the view

//this is the important part: here you are telling the centre of the button to be horizontally centre in the view
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 

//with this line you are telling the button to position itself vertically 100 from the bottom of the view. you can change the number to whatever suits your needs
button.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -100).isActive = true 

您还需要设置 button.widthAnchorbutton.heightAnchor,因为它们将不再设置在框架中。

重要提示

根据我的(公认有限的)经验,您应该确保在应用约束之前将按钮添加到视图中。

如果您同时使用框架和约束,您的应用可能会遇到一些布局错误。我建议现在是更改此视图上的任何框架以改用约束的好时机:)

我希望这个例子有效,因为我的 Xcode 决定放弃我,我不得不完全从记忆中编写这个。让我知道你是怎么相处的:P

,

嘿,这个代码可能会帮助你。

 button.frame = CGRect(x: self.view.bounds.width/2,y: self.view.bounds.height,height: 54)

只需在此处替换您的那一行,我们将使用此代码获取视图高度和宽度 self.view.bounds.height

对不起,如果我走错了路。 谢谢。

,

你必须在视图中而不是在imageView中设置约束和添加按钮,我没有看到你的imageView约束,所以我希望imageView不要被限制在整个视图的顶部和底部。在视图中添加按钮,给它约束,当您使用约束时,您可以跳过 std::streambuf 并添加 button.framewidthAnchor。最后激活它们并保持 heightAnchortranslatesAutoresizingMaskIntoConstraints

false

newImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant: .zero).isActive = true newImageView.topAnchor.constraint(equalTo: view.topAnchor,constant: .zero).isActive = true newImageView.heightAnchor.constraint(equalToConstant:600).isActive = true // 根据需要调整 self.navigationController?.isNavigationBarHidden = true self.tabBarController?.tabBar.isHidden = true

    @objc private func onButton(sender: UIButton){

    let imageView = sender.currentImage!
    let newImageView = UIImageView(image: imageView)
    newImageView.translatesAutoresizingMaskIntoConstraints = false
    newImageView.backgroundColor = .white
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self,action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap) 

    self.view.addSubview(newImageView)

    // New Image View Constraints 

    newImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: .zero).isActive = true

另外,如果能加上专有名词就更好了。此外,从长远来看,您应该创建一个 //Add In View & SomeButton Constraints view.addSubview(someButton) someButton.widthAnchor.constraint(equalToConstant: 150).isActive = true someButton.heightAnchor.constraint(equalToConstant: 40).isActive = true someButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true someButton.topAnchor.constraint(equalTo: newImageView.bottomAnchor,constant: 20).isActive = true } private lazy var someButton: UIButton = { let button = UIButton() button.translatesAutoresizingMaskIntoConstraints = false button.setTitle("Make Profile Pic",for: .normal) button.setTitleColor(.black,for: .normal) button.addTarget(self,action: #selector(onSomeButton(_:)),for: .touchUpInside) button.backgroundColor = .purple return button }() 以使用 extension 设置您的 constraints

P.S.:我检查了你的 newImageView 给全帧的约束,我已经更新了相同的约束。