How to make clickable a part of UIView - swift

I have a custom view with stackview inside. In my stack view contains Image, Label1, and Label2 horizontally.
I set my custom view with constraints with height = 25 and width = 160.
I need to tap only on a specific part of the view. How can I do it?
For example, only Label2 can be clickable.

#IBOutlet weak var label2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let gesture = UITapGestureRecognizer(target: self, action: #selector(click))
label2.isUserInteractionEnabled = true
label2.addGestureRecognizer(gesture)
}
#objc func click(sender:UITapGestureRecognizer) {
}

You might want to use GestureRecognizers https://developer.apple.com/documentation/uikit/touches_presses_and_gestures

Related

Strange cornerRadius behaviour on UIView from xib

I created a custom Numpad keyboard through xib and wanted to initialize it with a rounded corners.
Here is the code I use:
import UIKit
class NumpadView: UIView {
#IBOutlet weak var resetButton: NumpadButton!
#IBOutlet weak var decimalButton: NumpadButton!
var target: UITextInput?
var view: UIView?
init(target: UITextInput, view: UIView) {
super.init(frame: .zero)
self.target = target
self.view = view
initializeSubview()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initializeSubview()
}
func initializeSubview() {
let xibFileName = "NumpadView"
let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)![0] as! UIView
self.layer.cornerRadius = 30
self.layer.masksToBounds = true
self.addSubview(view)
view.frame = self.bounds
self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
But then I receive the strange view look in area where cornerRadius is implemented:
How to remove that grey background which is visible near the rounded corners?
UPDATE:
According to View Debugger it seems like this grey layer between yellow square and Visual Effect View is a UICompatibilityInputViewController:
How I presenting the Numpad:
1.I created a NumpadView as a UIView subclass in a xib:
2.In my VC I just change a standard textField.inputView property on my custom NumpadView:
import UIKit
class NumpadViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setupKeyboardHide()
textField.delegate = self
textField.inputView = NumpadView(target: textField, view: view)
}
}
Test project on Github
Another option to get your "rounded corners"...
Get rid of the Visual Effect View in your Numpad class, and set a layer mask on the superview at run-time.
In class NumpadView: UIView, UIInputViewAudioFeedback:
override func layoutSubviews() {
super.layoutSubviews()
guard let sv = superview else { return }
let maskLayer = CAShapeLayer()
let bez = UIBezierPath(roundedRect: bounds, cornerRadius: 16)
maskLayer.path = bez.cgPath
sv.layer.mask = maskLayer
}
Looks like this:
Sure, the NumpadView object has rounded corner. However, you are using it like this:
textField.inputView = NumpadView(target: textField, view: view)
So, that part which is not rounded is the textField's inputView. I'm not quite sure if you can modify its layer to have corner radius. But, if you really want to get that rounded corner effect, an easier approach is just to add the NumpadView directly to the parent view and anchored to the bottom. Then show it via the begin editing delegate of the textfield (and hide via end editing).

ScrollView Constraint problem threw storyboard

So I maybe successfully created a scroll view following a tutorial but something is terribly wrong. So I'm trying to create a scroll view with a table view inside and some buttons and labels and everything works fine but as soon as I add a single constraint it all goes just white with no explanation. I would assume content view is messing things up but I'm not sure, thx in advance!
So following some other people problems I tried filling constraints programmatically and doing views and subviews programmatically aswell, keep in mind tho that rest of constraints I did on storyboard. Btw I have tried equal width and height on newView --> scroll view and nothing seems to change.
my view hierarchy looks like this
myViewController -> View -> scrollView -> newView
class ThirdViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.addSubview(newView)
newView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
newView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
newView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
newView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
//tableView1.dataSource = self
//tableView1.delegate = self
//tableView1.register(UITableViewCell.self, forCellReuseIdentifier: "cell1")
}
#IBOutlet weak var newView: UIView!
override func viewWillLayoutSubviews() {
scrollView.contentSize = CGSize(width: 375, height: 1950)
}
}
Sorry, if question is layout pretty badly - I'm not that pro!

Similar UITapGestureRecognizer Actions for Many ImageView

I am relatively new to Swift.
I have many images (though right now I am testing with four) which I am trying to hide (temporarily to make sure the foundational code is working, instead I really want to insert an image under the tapped image) when they are tapped.
I have created an array of ImageViews which I plan to expand once I have working code. I tried to add UITapGestureRecognizers to each ImageView using a for loop in addGestures() and then have selectImage() hide the tapped ImageView. The code compiles without error, but fails with uncaught NSException when one of these images is tapped. Any tips on how to do this effectively without too much manual coding for each image?
Code attached
Please check below code
func addGestureRecognizer(){
for imageView in imageArray{
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(gesture:)))
imageView.addGestureRecognizer(tap)
}
}
#objc func handleTap(gesture:UITapGestureRecognizer){
let imageView = gesture.view
imageView?.isHidden = true
}
in your code you're passing imageView instead of gesture on selector.however you can get imageView by gesture.view.
Also there is no need for separate function for enable userInteraction.
Here is what you can try
class GestureStackVC: UIViewController {
/// Image Outlets
#IBOutlet weak var img1: UIImageView!
#IBOutlet weak var img2: UIImageView!
#IBOutlet weak var img3: UIImageView!
/// ImageView Array
var imagesArray : [UIImageView]?
/// Image Gesture
var imageTapGesture : UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
/// Allocate Array
imagesArray = [UIImageView]()
/// Add Required Values
imagesArray = [img1,img2,img3]
/// Add gesture
for imageView in imagesArray!{
let tap = UITapGestureRecognizer(target: self, action: #selector(imageTapHandler(_:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tap)
}
}
/// tap Handler
#objc func imageTapHandler(_ sender: UITapGestureRecognizer) {
/// Hide the Sender View
sender.view?.isHidden = true
/// Done
}
}
First Output with 3 Images
Output when clicked On first ImageView1 - Results its hidden
Output when clicked On first ImageView2 - Results its hidden

How do I completely hide this UIImageView

I have a UITableView with a list of items that can be filtered. When the user chooses a filter, a UIView is exposed using a height NSLayoutConstraint that changes from 0 to 40 pixels:
The circle with the X is the clear button that clears the filter and then closes the UIView by changing the height constraint back to 0.
The problem is that when the UIView closes, that clear button doesn't completely go away:
Here's the relevant code:
class LiftLogViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
let coreDataStack = CoreDataStack()
var liftEvents = [LiftEvent]()
//MARK: IB outlets
#IBOutlet var tableView: UITableView!
#IBOutlet weak var navItem: UINavigationItem!
#IBOutlet weak var filterViewHeightConstraint: NSLayoutConstraint!
#IBOutlet weak var clearFilterButton: UIImageView!
#IBOutlet weak var selectedFilter: UILabel!
#IBOutlet weak var clearButtonHeightConstraint: NSLayoutConstraint!
#IBOutlet weak var clearButtonView: UIImageView!
var isFilterViewOpen = false
override func viewDidLoad() {
let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(self.dismissLog(_:)))
let filterImage = UIImage(named: "filter_icon")
let filterButton = UIBarButtonItem(image: filterImage, style: .Plain, target: self, action: #selector(self.actionFilter))
self.navItem.rightBarButtonItems = [doneButton, filterButton]
let buttonTap = UITapGestureRecognizer(target: self, action: #selector(self.clearFilter))
clearFilterButton.addGestureRecognizer(buttonTap)
filterViewHeightConstraint.constant = 0.0
clearButtonHeightConstraint.constant = 0.0
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
let filterPredicate: NSPredicate?
if let logFilter = UserDefaultsManager.sharedInstance.logFilter?.rawValue {
filterPredicate = NSPredicate(format: "lift.liftName = [c] %#", logFilter)
print("viewWillAppear thinks the filter is \(logFilter)")
} else {
filterPredicate = nil
}
reloadData(filterPredicate)
let currentFilter = getCurrentLogFilter()
if currentFilter != nil {
selectedFilter.text = "Filtered by \(currentFilter!)"
isFilterViewOpen = true
clearButtonView.hidden = isFilterViewOpen ? false : true
} else {
selectedFilter.text = nil
}
super.viewWillAppear(animated)
}
override func viewDidAppear(animated: Bool) {
filterViewHeightConstraint.constant = isFilterViewOpen ? 40.0 : 0.0
clearButtonHeightConstraint.constant = isFilterViewOpen ? 21.0 : 0.0
clearButtonView.hidden = isFilterViewOpen ? false : true
UIView.animateWithDuration(0.33, delay: 0, options: [.CurveEaseOut], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
func clearFilter() {
UserDefaultsManager.sharedInstance.logFilter = nil
isFilterViewOpen = !isFilterViewOpen
UIView.animateWithDuration(0.33, delay: 0, options: [.CurveEaseOut], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
selectedFilter.text = nil
reloadData()
}
You can see I've tried both setting the .hidden value on the UIView that holds the clear button to true and I've tried changing the height constraint to 0.0 but neither of those make it gone completely.
Searching for a while hasn't produced any answers. Can anybody point out what's wrong with what I'm doing? Thanks.
It seems like the hair line color on the navigation bar may be clear resulting in the button still being slightly visible. Maybe change that and see if it covers the button. You can do so like this:
let view = UIView()
view.backgroundColor = UIColor.grayColor()
self.navigationController!.navigationBar.hairlineImageViewInNavigationBar(view)
As Dustin Spengler mentioned, the transparent hairline image view in the navigation bar causes the filter button to be slightly visible beneath it. As a workaround, you can hide the filter view (filterView.hidden = true) when constraint constant is set to 0.0 and unhide the filter view (filterView.hidden = false) when constraint constant is set to 40.0.
Thank you guys for your suggestions. It turns out that the solution was a little different, but your suggestions got me to poke around some more in that area and find the solution.
I neglected to mention that this was a view controller that I add a UIStackView that wrapped my UIView (the grey "filtered by" UIView) and the UITableView:
The problem was that the constraint I created from the top of the UIStackView to the top of its parent view was just slightly too big, pushing the top of the UIStackView just far enough to expose that little sliver. I reduced the Y distance to 63 and it's now perfect:
Thanks again for trying to help.

How to add constraints to a Custom UITableViewCell

I have created a custom cell for my table view and it works fine except that my Image View will not align correctly.
Is there a way to add constraints to a custom cell view? Programmatically or otherwise?
This is what my cell looks like in the storyboard - which is what I was hoping to achieve at run time:
But when I demo the app this is what happens:
Here is another image from the storyboard:
View Controller /w TableView
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
println(questions)
println(finalResults)
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
tableView.frame = CGRectMake(0,0, theHeight, theWidth)
}
Cell View Controller
override func awakeFromNib() {
let theWidth = UIScreen.mainScreen().bounds.width
contentView.frame = CGRectMake(0,0, theWidth, 64)
answerImage.center = CGPointMake(115, 15)
}
You only have to set up your constraints correctly. Indeed, even if Add Missing Constraints and Reset to Suggested Constraints are handy sometimes they don't know what you want, thus the result cannot always be what you expect.
What you might want to do here is the following.
For you question label set it in center Y of it's container and set it's leading space to the superview. Like so :
Then for you image, you want to set it in center Y of it's container too and set a ratio 1:1 on it. Like so :
Note that you might also want to check the width and height if you don't want your image to upscale (if you set a bigger cell on some device).
You should now have something like that :
And the result :
Let me know if it helped.
PS : I don't have your image so I've just set a background color on the uiimageview
Assuming that you started with a View Controller, Dragged a table and a Cell into the table...
in ViewController: UIViewController...{
#IBOutlet weak var resultsTable: UITableView! // connect to tableView
override func viewDidLoad() {
super.viewDidLoad()
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
resultsTable.frame = CGRectMake(0,0, theWidth, theHeight)
}
}
in UITableViewCell file:
#IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
let theWidth = UIScreen.mainScreen().bounds.width
contentView.frame = CGRectMake(0,0 theWidth, 64)
imageView.center = CGPointMake(115, 15) //mess around with these #
}