Ios rState:UIControl.State() actionButton.backgroundColor=.green actionButton.action={button.toggleMenu()中的按钮 //self.setBlurView() } } 覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){ 变量touch:UITouch?=touch.first 如果触摸?.view!=信息视图{ dismissView() } } func dismissView(){ //驳回(动画:真,完成:无) removeBlurView() } func setBlurView(){ 让blurView=UIVisualEffectView() blurView.frame=view.frame blurView.effect=UIBlurEffect(样式:。常规) blurView.autoresizingMask=[.flexibleWidth、.flexibleHeight] view.addSubview(blurView) } func呈现视图(带信息:字符串){ 第二视图() view.addSubview(infoView) //infoView.addView() 设img=UIImage(名称:info) infoView.imageView.image=img infoView.nameLbl.text=info infoView.backgroundColor=.white infoView.centerXAnchor.constraint(equalTo:view.centerXAnchor).isActive=true infoView.widthAnchor.constraint(equalTo:view.widthAnchor,乘数:0.8)。isActive=true //infoView.widthAnchor.constraint(equalToConstant:view.frame.width-64).isActive=true infoView.heightAnchor.constraint(equalTo:view.heightAnchor,乘数:0.8)。isActive=true //infoView.heightAnchor.constraint(equalToConstant:view.frame.height-64)。isActive=true infoView.centerYAnchor.constraint(等式:view.centerYAnchor,常数:-44)。isActive=true infoView.transform=CGAffineTransform(scaleX:1.3,y:1.3) infoView.alpha=0 UIView.animate(持续时间:0.5){ self.infoView.alpha=1 self.infoView.transform=.identity } } }

Ios rState:UIControl.State() actionButton.backgroundColor=.green actionButton.action={button.toggleMenu()中的按钮 //self.setBlurView() } } 覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){ 变量touch:UITouch?=touch.first 如果触摸?.view!=信息视图{ dismissView() } } func dismissView(){ //驳回(动画:真,完成:无) removeBlurView() } func setBlurView(){ 让blurView=UIVisualEffectView() blurView.frame=view.frame blurView.effect=UIBlurEffect(样式:。常规) blurView.autoresizingMask=[.flexibleWidth、.flexibleHeight] view.addSubview(blurView) } func呈现视图(带信息:字符串){ 第二视图() view.addSubview(infoView) //infoView.addView() 设img=UIImage(名称:info) infoView.imageView.image=img infoView.nameLbl.text=info infoView.backgroundColor=.white infoView.centerXAnchor.constraint(equalTo:view.centerXAnchor).isActive=true infoView.widthAnchor.constraint(equalTo:view.widthAnchor,乘数:0.8)。isActive=true //infoView.widthAnchor.constraint(equalToConstant:view.frame.width-64).isActive=true infoView.heightAnchor.constraint(equalTo:view.heightAnchor,乘数:0.8)。isActive=true //infoView.heightAnchor.constraint(equalToConstant:view.frame.height-64)。isActive=true infoView.centerYAnchor.constraint(等式:view.centerYAnchor,常数:-44)。isActive=true infoView.transform=CGAffineTransform(scaleX:1.3,y:1.3) infoView.alpha=0 UIView.animate(持续时间:0.5){ self.infoView.alpha=1 self.infoView.transform=.identity } } },ios,swift,floating-action-button,Ios,Swift,Floating Action Button,我试图给视图和浮动动作按钮添加一个约束,但它导致了整个过程崩溃。我还尝试添加一个函数,该函数在设备方向更改时删除当前约束,并为特定方向添加一个新约束,但没有真正的区别 如果我能提供更多信息,请询问。 多谢各位 更新此约束 我的fork源代码和更新 浮动按钮的第一件事,在iPhoneX,11。。。安全区域比正常区域更大,因此我们必须使用safeAreaLayoutGuide添加约束,如以下代码: /// the float button's trailing padding filepri

我试图给视图和浮动动作按钮添加一个约束,但它导致了整个过程崩溃。我还尝试添加一个函数,该函数在设备方向更改时删除当前约束,并为特定方向添加一个新约束,但没有真正的区别

如果我能提供更多信息,请询问。 多谢各位

更新此约束


我的fork源代码和更新

浮动按钮的第一件事,在iPhoneX,11。。。安全区域比正常区域更大,因此我们必须使用
safeAreaLayoutGuide
添加约束,如以下代码:

/// the float button's trailing padding
    fileprivate let floatButtonTrailingPadding: CGFloat = 15

/// the float button's bottom padding
fileprivate let floatButtonBottomPadding: CGFloat = 15

if #available(iOS 11.0, *) {
    let guide = self.parentView.safeAreaLayoutGuide
    self.floatButton.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -(floatButtonTrailingPadding)).isActive = true
    self.floatButton.bottomAnchor.constraint(equalTo: guide.bottomAnchor, constant: floatButtonBottomPadding).isActive = true
}
else {
    let trailingSpacing = NSLayoutConstraint.constraints(withVisualFormat: "V:[floatButton]-\(floatButtonTrailingPadding)-|", options: NSLayoutConstraint.FormatOptions.alignAllCenterX, metrics: nil, views: views)
    let bottomSpacing = NSLayoutConstraint.constraints(withVisualFormat: "H:[floatButton]-\(floatButtonBottomPadding)-|", options: NSLayoutConstraint.FormatOptions.alignAllCenterX, metrics: nil, views: views)
    self.parentView.addConstraints(trailingSpacing)
    self.parentView.addConstraints(bottomSpacing)
}
第二件事是设备是纵向的,单击按钮并显示菜单列表,将设备旋转到横向,然后菜单列表消失,但实际上
模糊了视觉效果,菜单列表仍然存在,但当设备旋转时,菜单列表不更新y位置

我的解决方案是在
blurVisualEffect
vs之间添加约束
contentView
contentView
vs
parentView
。我还添加了约束 对于
contentView

查看更多更新详细信息

如果有任何问题,请告诉我


希望这有帮助

您好,我以前更新过约束,但它仍然不起作用。非常感谢。
import Foundation
import UIKit

public typealias CameraButtonItemAction = (CameraButtonItem) -> (Void)


open class CameraButtonItem: NSObject {

    //the action the item should perform when tapped
    open var action: CameraButtonItemAction?

    //Description of the item's action
    open var text: String {
        get {
            return self.label.text!
        }
        set {
            self.label.text = newValue
        }
    }

    //view that will hold the item's button and label
    var view: UIView!

    //label that contain the item's text
    var label: UILabel!

    //main button that will perform the defined action
    var button: UIButton!

    //image used by the button
    var image: UIImage!

    //size needed for the view property present the item's content
    let viewSize = CGSize(width: 200, height: 35)

    //button's size by default the button is 35x35
    let buttonSize = CGSize(width: 35, height: 35)

    var labelBackground: UIView!
    let backgroundInset = CGSize(width: 10, height: 10)

    public init(title optionalTitle: String?, image: UIImage?) {
        super.init()
        //so when the button is pressed a view will appear with buttons and labels that can be pressed. The view has no background color.
        self.view = UIView(frame: CGRect(origin: CGPoint.zero, size: self.viewSize))
        self.view.alpha = 0
        self.view.isUserInteractionEnabled = true
        //self.view.backgroundColor = UIColor.purple


        //this creates the button that we can press
        self.button = UIButton(type: .custom)
        self.button.frame = CGRect(origin: CGPoint(x: self.viewSize.width - self.buttonSize.width, y: 0), size: buttonSize)
        self.button.layer.shadowOpacity = 1
        self.button.layer.shadowRadius = 2
        self.button.layer.shadowOffset = CGSize(width: 1, height: 1)
        self.button.layer.shadowColor = UIColor.black.cgColor
        self.button.addTarget(self, action: #selector(CameraButtonItem.buttonPressed(_:)), for: .touchUpInside)

        if let unwrappedImage = image {
            self.button.setImage(unwrappedImage, for: UIControl.State())
        }

        if let text = optionalTitle, text.trimmingCharacters(in: CharacterSet.whitespaces).isEmpty == false {
            self.label = UILabel()
            self.label.font = UIFont(name: "HelveticaNeue-Medium", size: 13)
            self.label.textColor = UIColor.black
            self.label.textAlignment = .center
            self.label.text = text
            self.label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(CameraButtonItem.labelTapped(_:))))
            self.label.sizeToFit()

            self.labelBackground = UIView()
            self.labelBackground.frame = self.label.frame
            self.labelBackground.backgroundColor = UIColor.white
            self.labelBackground.layer.cornerRadius = 5
            self.labelBackground.layer.shadowOpacity = 0.8
            self.labelBackground.layer.shadowOffset = CGSize(width: 0, height: 1)
            self.labelBackground.layer.shadowRadius = 0.2
            self.labelBackground.layer.shadowColor = UIColor.lightGray.cgColor

            //Adjust the label's background inset
            self.labelBackground.frame.size.width = self.label.frame.size.width + backgroundInset.width
            self.labelBackground.frame.size.height = self.label.frame.size.height + backgroundInset.height
            self.label.frame.origin.x = self.label.frame.origin.x + backgroundInset.width / 2
            self.label.frame.origin.y = self.label.frame.origin.y + backgroundInset.height / 2

            //adjust the label's background position
                //distance between the button and the label
            self.labelBackground.frame.origin.x = CGFloat(130 - self.label.frame.size.width)
            self.labelBackground.center.y = self.view.center.y
            self.labelBackground.addSubview(self.label)

            //Add Tap Gesture Recognizer
            let tap = UITapGestureRecognizer(target: self, action: #selector(CameraButtonItem.labelTapped(_:)))
            self.view.addGestureRecognizer(tap)

            self.view.addSubview(self.labelBackground)
        }
        self.view.addSubview(self.button)
    }


    //MARK: -Button Action Methods
    @objc func buttonPressed(_ sender: UIButton) {
        if let unwrappedAction = self.action {
            unwrappedAction(self)
        }
    }

    //MARK: - Gesture Recognizer Methods
    @objc func labelTapped(_ gesture: UIGestureRecognizer) {
        if let unwrappedAction = self.action {
            unwrappedAction(self)
        }
    }


}
import UIKit

extension ViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 150)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
}


extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TestingCollectionViewCell
        cell.backgroundColor = .white
        let img = UIImage(named: self.items[indexPath.row])
        cell.imageView.image = img
        cell.imageName.text = "\(self.items[indexPath.row])"
        return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("\(items[indexPath.row])")
        self.presentInfoView(withInfo: items[indexPath.row])
    }
}

class ViewController: UIViewController, BlurDelegate {

    var actionButton: CameraButton!




    func removeBlurView() {
        for subview in view.subviews {
            if subview.isKind(of: UIVisualEffectView.self) {
                subview.removeFromSuperview()
                self.infoView.removeFromSuperview()
            }
        }
    }


    fileprivate var items: [String] = [
        "photo1",
        "photo2",
        "photo3",
        "photo4",
        "photo5",
        "photo6",
        "photo7",
        "photo8",
        "photo9",
        "photo10",
        "photo11",
        "photo12",
        "photo13",
        "photo14",
        "photo15",
        "photo16",
        "photo17",
        "photo18",
        "photo19",
        "photo20",
        "photo21",
        "photo22",
        "photo23",
        "photo24"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        let collection = UICollectionView(frame: view.frame, collectionViewLayout: UICollectionViewFlowLayout())
                //allows us to use auto layout constraints
        collection.translatesAutoresizingMaskIntoConstraints = false
        collection.backgroundColor = .black
        view.addSubview(collection)
        collection.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        collection.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        collection.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        collection.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

        collection.dataSource = self
        collection.delegate = self

        collection.register(TestingCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        self.navigationItem.title = "Testing"
        setUpButton()

    }

    func setUpButton() {
        let cameraBtn = UIImage(named: "CameraBtn")
        let photoBtn = UIImage(named: "PhotoBtn")
        let cameraRound = UIImage(named: "otherCamera")
        let camera = CameraButtonItem(title: "Camera", image: cameraBtn)
        let photo = CameraButtonItem(title: "Photo", image: photoBtn)
        camera.action = { item in
            print(item)
            print("Camera Btn Pressed")
        }
        photo.action = { item in
            print(item)
            print("Photo Btn Pressed")
        }

        actionButton = CameraButton(attachedToView: self.view, items: [camera, photo])
        actionButton.setImage(cameraRound, forState: UIControl.State())
        actionButton.backgroundColor = .green
        actionButton.action = { button in button.toggleMenu()
            //self.setBlurView()
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        var touch: UITouch? = touches.first
        if touch?.view != infoView {
            dismissView()

        }
    }




    func dismissView() {

        //dismiss(animated: true, completion: nil)
        removeBlurView()

    }

    func setBlurView() {
        let blurView = UIVisualEffectView()
        blurView.frame = view.frame
        blurView.effect = UIBlurEffect(style: .regular)
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(blurView)
    }

    func presentInfoView(withInfo info: String) {
        setBlurView()
        view.addSubview(infoView)
        //infoView.addView()
        let img = UIImage(named: info)
        infoView.imageView.image = img
        infoView.nameLbl.text = info


        infoView.backgroundColor = .white
        infoView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        infoView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8).isActive = true
        //infoView.widthAnchor.constraint(equalToConstant: view.frame.width - 64).isActive = true
        infoView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.8).isActive = true
        //infoView.heightAnchor.constraint(equalToConstant: view.frame.height - 64).isActive = true
        infoView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -44).isActive = true
        infoView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)

        infoView.alpha = 0

        UIView.animate(withDuration: 0.5) {
            self.infoView.alpha = 1
            self.infoView.transform = .identity
        }
    }

}
 let trailingSpacing = NSLayoutConstraint.constraints(withVisualFormat: "V:[floatButton]-20-|", options: NSLayoutConstraint.FormatOptions.alignAllCenterX, metrics: nil, views: views)
 let bottomSpacing = NSLayoutConstraint.constraints(withVisualFormat: "H:[floatButton]-25-|", options: NSLayoutConstraint.FormatOptions.alignAllCenterX, metrics: nil, views: views)
/// the float button's trailing padding
    fileprivate let floatButtonTrailingPadding: CGFloat = 15

/// the float button's bottom padding
fileprivate let floatButtonBottomPadding: CGFloat = 15

if #available(iOS 11.0, *) {
    let guide = self.parentView.safeAreaLayoutGuide
    self.floatButton.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -(floatButtonTrailingPadding)).isActive = true
    self.floatButton.bottomAnchor.constraint(equalTo: guide.bottomAnchor, constant: floatButtonBottomPadding).isActive = true
}
else {
    let trailingSpacing = NSLayoutConstraint.constraints(withVisualFormat: "V:[floatButton]-\(floatButtonTrailingPadding)-|", options: NSLayoutConstraint.FormatOptions.alignAllCenterX, metrics: nil, views: views)
    let bottomSpacing = NSLayoutConstraint.constraints(withVisualFormat: "H:[floatButton]-\(floatButtonBottomPadding)-|", options: NSLayoutConstraint.FormatOptions.alignAllCenterX, metrics: nil, views: views)
    self.parentView.addConstraints(trailingSpacing)
    self.parentView.addConstraints(bottomSpacing)
}