В моем UITableView я хочу иметь возможность щелкнуть кнопку с фиксированной позицией (см. Прикрепленное изображение). Но все, что находится впереди или за заголовками разделов UITableView, блокируется от взаимодействия, оно не регистрирует нажатия кнопок. Я попытался установить более высокое zPosition, которое приносит кнопки перед заголовками, но я все еще не могу нажать кнопки. Это то, что он lookes как:Swift - заголовок в взаимодействии кнопок блоков UITableView
Image that shows the buttons before and after (nameOfButton).layer.zPosition = 99
is applied.
Даже если мои кнопки находится в передней части заголовка, кнопка отводы не зарегистрирован ...
Я создаю кнопки программно, и мой viewDidLoad
lookes, как это:
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, (0 + self.tableView.contentOffset.y + UIScreen.mainScreen().bounds.height), (UIScreen.mainScreen().bounds.width - 40), 40)
cancelButton.frame = CGRectMake(20, (0 + self.tableView.contentOffset.y + UIScreen.mainScreen().bounds.height), (UIScreen.mainScreen().bounds.width - 40), 40)
takePhotoButton.backgroundColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1.0)
photoLibraryButton.backgroundColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1.0)
cancelButton.backgroundColor = UIColor(red: 0/255, green: 122/255, blue: 255/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, и не знаю другого решения для кнопок в таблицеView.
Это сработало отлично! Если я использовал его в методе 'willDisplayHeaderView', кнопки работают. –