【问题标题】:how to hide button in customView in swift如何快速隐藏自定义视图中的按钮
【发布时间】:2019-01-04 10:38:45
【问题描述】:

我想在点击屏幕时隐藏第三行的按钮,这个屏幕包含三行。我添加了触摸事件并尝试使用轻击手势,但只有当我点击第三行时,如果我点击其他行或点击屏幕的任何位置,按钮就会隐藏,没有任何反应。 主要问题是当我点击第三行然后按钮消失它应该被隐藏我们点击屏幕上的任何地方或点击任何单元格。 我正在使用 customView 在 tablewView 中显示该视图。 TableView 是单独的类,自定义视图是单独的类。我正在处理自定义视图类中的按钮。

请告诉我解决方案,这是我的代码

class MicTestView: UIView  {

// MARK:-
// MARK:- Properties

// MARK:- IBOutlets

@IBOutlet weak var aidStatusImageView: UIImageView!
@IBOutlet weak var progressView: UIProgressView!

// MARK:- Class Properties

var micLocation : MicLocation = .bottom
let customInfoView = CustominfoView()

var customView = UIView()

// MARK:-
// MARK:- Methods

// MARK: UIView Methods

required init(coder aDecoder : NSCoder) {
    super.init(coder :aDecoder)!
    setup()
}

override init(frame : CGRect) {
    super.init(frame: frame)
    setup()
}

func setup() {
    let view = loadViewFromXib()
    view.frame = bounds
    // view.autoresizingMask = UIViewAutoresizing.flexibleWidth | UIViewAutoresizing.flexibleHeight

    addSubview(view)

    progressView.progress = 0
    progressView.transform = progressView.transform.scaledBy(x: 1, y: (view.superview?.bounds.height)!/2)
    progressView.isHidden = true
    view.isUserInteractionEnabled = true
}

func loadViewFromXib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "MicTestView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
    return view
}

// MARK:- IBActions
@IBAction func infoBtnpressed(_ sender: UIButton) {
    customInfoView.isHidden = false

    let customView = customInfoView
    customView.frame = CGRect(x: ((window?.frame.origin.x)! + 60), y: ((window?.frame.origin.y)! + 300), width: 255, height: 200)
    window?.addSubview(customView)
    print("Pressed")

    DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
        self.customInfoView.isHidden = true
    }
}

// MARK: UIViewController overrides
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    self.infoBtn.isHidden = true
    self.customInfoView.isHidden = true
    startRecording()

}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    //
}

【问题讨论】:

  • 你可以使用协议委托来隐藏uitableview中的按钮。
  • 请给我代码示例

标签: ios swift uiview xib custom-view


【解决方案1】:

自定义单元格

protocol tappedDelegate: class {
    func buttonTapped(indexpath: NSIndexPath,btnChange: UIButton)
}

class CustomCell: UITableViewCell {
    var delegate: tappedDelegate?
    var  indexpath1 = NSIndexPath()
    var isOn = Bool()

    @IBOutlet weak var btnClick: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    @IBAction func someButtonTapped(sender: UIButton) {
        let btnImag = btnClick.image(for: .normal)

        if btnImag == UIImage (named: "close") {
            btnClick .setImage(UIImage(named: "buttn"), for: .normal)
        }else
        {
            btnClick .setImage(UIImage(named: "close"), for: .normal)

        }

        self.delegate?.buttonTapped(indexpath: indexpath1, btnChange: btnClick)
    }

class ViewController: UIViewController , tappedDelegate


   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "CustomCell"


        var cell: CustomCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustomCell

        if cell == nil {
            tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustomCell
        }

        cell.delegate = self
        cell.indexpath1 = indexPath as NSIndexPath

        return cell
    }
    func buttonTapped(indexpath: NSIndexPath, btnChange: UIButton) {

   // btnChange .setImage(UIImage(named: "buttn"), for: .normal)

    }

【讨论】:

  • 始终包含解决方案的说明,而不仅仅是代码!
猜你喜欢
  • 1970-01-01
  • 2020-07-03
  • 2015-07-15
  • 2022-08-16
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 2015-07-17
  • 2016-09-02
相关资源
最近更新 更多