I have a button that is showing but not responding to touches. This button is created in code, inside a UIView that is also created in code. On my device screen, the button appears to be off from where it is supposed to be, however, I cannot find any reason to suspect I (I'm a team of one) am at fault for this. I was unable to successfully use the view debugger. In case this is a bug, I have already filed a report with Apple. I already reinstalled Xcode. No views are set to overlap.
Here's my code.
Here's my code.
Code:
//
// MenuItemView.swift
// Reading Expressway
//
// Created by Montana Burr on 8/10/16.
// Copyright © 2016 Montana. All rights reserved.
//
import UIKit
enum ActivityStatus {
case NotThereYet
case Next
case Completed
}
class MenuItemView: UIView {
var currentStatus : ActivityStatus = ActivityStatus.NotThereYet
var starNum : Int = 1
let button: UIButton! = UIButton()
@IBOutlet var starImageView: UIImageView? {
didSet {
// Load star.
let mainBundle = Bundle(for: MenuItemView.self)
let starURL = mainBundle.path(forResource: "\(starNum)star", ofType: "png", inDirectory:"pictures")
let starImage = UIImage(contentsOfFile: starURL!)
starImageView!.image = starImage!
button.bringSubview(toFront: button)
// Hide view
self.starImageView!.isHidden = true
// Set star so that it doesn't stretch.
self.starImageView!.contentMode = UIViewContentMode.scaleAspectFit
}
}/** Star image view. Defined in storyboard **/
func buttonPressed() {
(superview as! ExerciseMenuView).buttonPressed(sender: self)
}
init(frame: CGRect,_tag: Int)
{
super.init(frame: frame)
tag = _tag;
starImageView = UIImageView(frame: bounds)
addSubview(starImageView!)
addPlayButton()
}
func addPlayButton() // Adds the "Play" button
{
// Add button to self.
self.addSubview(button)
// Give the button its image.
let mainBundle = Bundle(for: MenuItemView.self)
let playURL = mainBundle.path(forResource: "play-button", ofType: "png", inDirectory:"pictures")
let playImage = UIImage(contentsOfFile: playURL!)
button.setBackgroundImage(playImage, for: UIControlState.normal)
button.isUserInteractionEnabled = false
// Respond to button taps
button.addTarget(self, action: #selector(MenuItemView.buttonPressed), for: UIControlEvents.touchUpInside)
// Make sure button doesn't accidentally resize
button.translatesAutoresizingMaskIntoConstraints = true
// Size button to fit in view.
let buttonOrigin = CGPoint(x:0, y:bounds.maxY)
let buttonSize = CGSize(width: bounds.size.width, height: 128)
button.frame = CGRect(origin: buttonOrigin, size: buttonSize)
// Size button image to fit
button.contentMode = UIViewContentMode.scaleAspectFit
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addPlayButton()
}
override init(frame: CGRect)
{
super.init(frame: frame)
}
func setActivityStatus(newStatus : ActivityStatus) {
self.isHidden = false
currentStatus = newStatus
switch newStatus {
case .Next:
button.isHidden = false
button.isUserInteractionEnabled = true
case .Completed:
starImageView!.isHidden = false
button.isUserInteractionEnabled = false
button.isHidden = true
default:
button.isHidden = true
starImageView!.isHidden = true
break
}
// Log
if (self.tag == 2)
{
print("setActivityStatus: Star image view superview is \(starImageView?.superview)")
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}