How can I click a button behind a transparent UIView?
Create a custom view for your container and override the pointInside: message to return false when the point isn't within an eligible child view, like this:
Swift:
class PassThroughView: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
for subview in subviews {
if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {
return true
}
}
return false
}
}
Objective C:
@interface PassthroughView : UIView
@end
@implementation PassthroughView
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *view in self.subviews) {
if (!view.hidden && view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event])
return YES;
}
return NO;
}
@end
Using this view as a container will allow any of its children to receive touches but the view itself will be transparent to events.
iPad: How can I click buttons underneath transparent portions of a UIView
-pointInside:withEvent:
is how iOS asks if a touch is within a particular view. If a view returns YES, iOS calls -hitTest:withEvent:
to determine the particular subview of that view that was touched. That method will return self
if there are no subviews at the location of that touch. So you can pass any touches that aren't on subviews back to views behind this one by implementing -pointInside:withEvent:
like this:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return ([self hitTest:point withEvent:event] != self);
}
If you need to catch some touches that aren't on subviews, your implementation will be more complicated, but this method is still the right place to tell iOS where your view is and accepts touch events.
Add UIView with Buttons and make it transparent and enable touch view behind that UIVIew
hey mate instead of add child view just add this button on your this same view and give backgroundColor clear and then in this bellow method try this..
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[touch locationInView:yourView];
else if([touch.view isKindOfClass:[UIImageView class]])
{
UIImageView *tempImage=(UIImageView *) touch.view;
[yourView bringSubviewToFront:tempImage];
[yourView bringSubviewToFront:yourButtons];/// set superview as a buttons..
}
}
i hope this help you..
How do I click a button behind a full-screen UIView?
Use
class TransView:UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return false
}
}
then
lazy var contentViewOutlet : TransView = {
let myUiView = TransView()
return myUiView
}()
How to create a view with a transparent view on it and background view touchable?
My action sheet is a custom View, I used a Button on view above the action sheet. After disabling user interaction and setting alpha .7 worked for me .
Make UIView Transparent to Only Certain Views
I don't really get your question, but I will give you a solution the link you have provided.
So in the link, we have a big transparent circle and we wish to block the small circle in the background right.
What you can is this.
You create two identical views.
First view is your normal view, you display whatever you want on it. The second you will be a backgroundView which is of the same position and same size as the first view. But the catch is this, the backgroundView will be displaying the same background color as the real background view.
So the view hierarchy will be like this
- Display View (Circle shape)
- BackgroundView (Circle size, same position as first view, same color as real background view, in this case color Blue)
- Real BackgroundView (Color blue ).
then all you have to do is keep the alpha value of the backgroundView as 1 and change the alpha value of the first view to, let's say, 0.6
sample code
private func addCircle(){
// adding a blue circle
let circleView = UIView()
circleView.backgroundColor = .blue
circleView.alpha = 0.5
view.addSubview(circleView)
circleView.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
circleView.center = view.center
circleView.layer.cornerRadius = 100 / 2
circleView.layer.masksToBounds = true
// adding a backgroundView
let backgroundView = UIView()
backgroundView.backgroundColor = .red
view.insertSubview(backgroundView, belowSubview: circleView)
backgroundView.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
backgroundView.center = view.center
backgroundView.layer.cornerRadius = 100 / 2
backgroundView.layer.masksToBounds = true
}
How to make a UIView transparent while presenting it from UITableViewCell with UITapGestureRecognizer?
Try This
let undectableVC = UndetectableAlertVC(nibName: "UndetectableAlertVC", bundle: nil)
undectableVC.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
undectableVC.modalPresentationStyle = .custom
undectableVC.modalTransitionStyle = .crossDissolve
self.navigationController?.present(undectableVC, animated: true, completion: nil)
Related Topics
Exhaustive Condition of Switch Case in Swift
Static Table View Outside Uitableviewcontroller
Programmatically Change Rootviewcontroller of Storyboard
Get Progress from Datataskwithurl in Swift
How to Access Own Window Within Swiftui View
How to Call Gesture Tap on Uiview Programmatically in Swift
The Code Signature Version Is No Longer Supported
How to Run/Test My Flutter App on a Real Device
Decoding JSON Web Tokens in Swift
Callback Method If User Declines Push Notification Prompt
Presenting a Uialertcontroller Properly on an iPad Using iOS 8
Capturing and Storing a Picture Taken with the Camera into a Local Database/Phonegap/Cordova/Ios
Arkit - Apply Cifilter to a Specific Vertices of Arfaceanchor
Swift Apply .Uppercasestring to Only the First Letter of a String