HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Swift – UITableView中的标题阻止按钮交互大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的UITableView中,我希望有一个选项可以单击一个具有固定位置的按钮(参见附图).但是UITableView节标题前面或后面的任何内容都被阻止了交互,它没有注册按钮点击.我试图设置一个更高的zPosition,它将按钮放在标题前面,但我仍然无法按下按钮.这就是它的样子:

Image that shows the buttons before and after (nameOfButton).layer.zPosition = 99 is applied.

即使我的按钮位于标题前面,按钮点按也没有注册

我正在以编程方式创建按钮,我的viewDidLoad看起来像这样:

override func viewDidLoad() {
        super.viewDidLoad()

        // Allow iOS to resize the cells automatically according to our Auto Layout constraints
        tableView.rowHeight = UITableViewAutomaticDimension

        // We will take ownership of the header view we've so nicely setup in the storyboard and then remove it from the table view.
        headerView = tableView.tableHeaderView
        tableView.tableHeaderView = nil
        tableView.addSubview(headerView)
        let addPhotoTapGesture = UITapGestureRecognizer(target: self,action: #selector(ViewController.addPhoto(_:)))
        headerView.addGestureRecognizer(addPhotoTapGesture)

        // PhotoPicker
        createPhotoPickerButtons()

        let effectiveHeight = kTableHeaderHeight-kTableHeaderCutAway/2
        tableView.contentInset = UIEdgeInsets(top: effectiveHeight,left: 0,bottom: 0,right: 0)
        tableView.contentOffset = CGPoint(x: 0,y: -effectiveHeight)

        headerMaskLayer = CAShapeLayer()
        headerMaskLayer.fillColor = UIColor.blackColor().CGColor

        headerView.layer.mask = headerMaskLayer
        updateHeaderView()
    }

还有我的createPhotoPickerButtons()(这里我将按钮放在视图下方,这样我就可以使用showImagePicker()为它们的外观设置动画:

func createPhotoPickerButtons() {

        takePhotoButton = UIButton.init(type: UIButtonType.System) // .System
        photoLibraryButton = UIButton.init(type: UIButtonType.System) // .System
        cancelButton = UIButton.init(type: UIButtonType.System) // .System

        takePhotoButton.frame = CGRectMake(20,(0 + self.tableView.contentOffset.y + UIScreen.mainScreen().bounds.height),(UIScreen.mainScreen().bounds.width - 40),40)
        photoLibraryButton.frame = CGRectMake(20,40)
        cancelButton.frame = CGRectMake(20,40)

        takePhotoButton.backgroundColor = UIColor(red: 0/255,green: 122/255,blue: 255/255,alpha: 1.0)
        photoLibraryButton.backgroundColor = UIColor(red: 0/255,alpha: 1.0)
        cancelButton.backgroundColor = UIColor(red: 0/255,alpha: 1.0)

        takePhotoButton.setTitle("Take Photo",forState: UIControlState.Normal)
        photoLibraryButton.setTitle("Photo Library",forState: UIControlState.Normal)
        cancelButton.setTitle("Cancel",forState: UIControlState.Normal)

        takePhotoButton.titleLabel?.font = UIFont.systemFontOfSize(17)
        photoLibraryButton.titleLabel?.font = UIFont.systemFontOfSize(17)
        cancelButton.titleLabel?.font = UIFont.systemFontOfSize(17)

        takePhotoButton.setTitleColor(UIColor.whiteColor(),forState: UIControlState.Normal)
        photoLibraryButton.setTitleColor(UIColor.whiteColor(),forState: UIControlState.Normal)
        cancelButton.setTitleColor(UIColor.whiteColor(),forState: UIControlState.Normal)

        let takePhotoButtonTapGesture = UITapGestureRecognizer(target: self,action: #selector(ViewController.takePhotoButtonAction(_:)))
        takePhotoButton.addGestureRecognizer(takePhotoButtonTapGesture)
        let photoLibraryButtonTapGesture = UITapGestureRecognizer(target: self,action: #selector(ViewController.photoLibraryButtonAction(_:)))
        photoLibraryButton.addGestureRecognizer(photoLibraryButtonTapGesture)
        let cancelButtonTapGesture = UITapGestureRecognizer(target: self,action: #selector(ViewController.cancelButtonAction(_:)))
        cancelButton.addGestureRecognizer(cancelButtonTapGesture)

        self.tableView.addSubview(takePhotoButton)
        self.tableView.addSubview(photoLibraryButton)
        self.tableView.addSubview(cancelButton)

        takePhotoButton.layer.zPosition = 99
        photoLibraryButton.layer.zPosition = 99
        cancelButton.layer.zPosition = 99

        takePhotoButton.alpha = 0
        photoLibraryButton.alpha = 0
        cancelButton.alpha = 0

    }

最后我的showImagePicker()在addPhoto()中调用(当用户点击图像时调用的动作):

func showImagePicker(){

        // (0 + self.tableView.contentOffset.y + UIScreen.mainScreen().bounds.height - 50 - 10 - 50 - 10 - 50 - 10)

        UIView.animateWithDuration(0.5) {
            self.takePhotoButton.alpha = 1
            self.photoLibraryButton.alpha = 1
            self.cancelButton.alpha = 1

            self.takePhotoButton.frame.origin.y = 0 + self.tableView.contentOffset.y + UIScreen.mainScreen().bounds.height - 50 - 10 - 50 - 10 - 50 - 10
            self.photoLibraryButton.frame.origin.y = 0 + self.tableView.contentOffset.y + UIScreen.mainScreen().bounds.height - 50 - 10 - 50 - 10
            self.cancelButton.frame.origin.y = 0 + self.tableView.contentOffset.y + UIScreen.mainScreen().bounds.height - 50 - 10
        }
    }

我在SO上找不到任何关于这个的东西,也不知道tableView中按钮的另一个解决方案.

解决方法

我花了一段时间才找到解决方案,但我弄清楚了:

sectionView.userInteractionEnabled = false

 

sectionView.layer.zPosition = -1

编辑:SWIFT 3更新:

view.isUserInteractionEnabled = false

大佬总结

以上是大佬教程为你收集整理的ios – Swift – UITableView中的标题阻止按钮交互全部内容,希望文章能够帮你解决ios – Swift – UITableView中的标题阻止按钮交互所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: