Image disappeared when tapped on UIButton using ZFRippleButton library - swift - ios

I am creating an sample app which integrated with library named ZFRippleButton . Now i am facing problem which when i tapped on button then image has been disappeared but when i take my finger out then the image is back as normal.
What is the problem?
Here is what i have done so far:
let button = ZFRippleButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.cornerRadius = 5
button.rippleColor = UIColor.red
button.backgroundColor = UIColor.red
button.setTitle("File Server", for: .normal)
button.setImage(UIImage(named: "filesharing"), for: .normal)
button.titleLabel?.font = UIFont(name: "SFCompactText-Regular", size: 16)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
Here is its action:
button.addTarget(self, action: #selector(fileShareButton), for: .touchUpInside)
#objc
func fileShareButton() {
print("Doing some actions")
}
I cannot figure it out is that when i tapped on that button and then image has been disappeared and when i released my finger and image has appeared as usual. But title never disappear like image.
Can you solve me with this issue?

Related

When i click on my buttons in my app that i have built using Swift and xcode they dont work

Ive added some buttons to a profile page on my app using swift but when i run my app they don't work. They appear on the page but they don't allow the user to click them and i don't know why this is my code that i am using:
in my profileHeader Class:
let editProfileFollowButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Edit Profile", for: .normal)
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.black.cgColor
button.backgroundColor = UIColor.black
button.layer.borderWidth = 1
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(handleEditProfileFollowButton), for: .touchUpInside)
return button
}()
#objc func handleEditProfileFollowButton() {
delegate?.handleEditFollowTapped(for: self)
}
in my profileVC:
func handleEditFollowTapped(for header: ProfileHeader) {
print("handle edit follow tapped")
}
so when the button is pressed its supposed to print "handle edit follow tapped" but it does nothing and im not sure why.
Even if i do it in the same class like this:
let editProfileFollowButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Edit Profile", for: .normal)
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.black.cgColor
button.backgroundColor = UIColor.black
button.layer.borderWidth = 1
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(handleEditProfileFollowButton), for: .touchUpInside)
return button
}()
#objc func handleEditProfileFollowButton() {
print("handle edit follow tapped")
}
it doesnt work.
this is how i display the button:
addSubview(editProfileFollowButton)
editProfileFollowButton.anchor(top: profileBanner.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 12, paddingLeft: 75, paddingBottom: 0, paddingRight: 75, width: 0, height: 30)
this is what it looks like when it is displayed:
You need to initialize editProfileFollowButton lazily because when you're declaring the editProfileFollowButton self (ProfileHeader class in your case) is still not available. Here's how
Modify this:
let editProfileFollowButton: UIButton = {
To this:
lazy var editProfileFollowButton: UIButton = {

why can't I tap uibutton in simulator but I can on my hardware iPhone?

I have two buttons on the top of my view controller, which is modally presented. The strange thing is that the buttons are not at all selectable when I click on them in the simulator.
The buttons cause no issues when I use them on my iPhone.
btn.addTarget(self, action: #selector(sendTapped), for: .touchUpInside)
That's the way I add my function to the button
Whole button code:
let post: UIButton = {
let btn = UIButton()
btn.frame = CGRect(x: 0, y: 0, width: 60, height: 30)
btn.setTitleColor(UIColor.lightGray.adjust(by: 20), for: .normal)
btn.isEnabled = false
btn.setTitle("Post", for: .normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .bold)
btn.addTarget(self, action: #selector(sendTapped), for: .touchUpInside)
return btn
}()
Looks like what I had to do was change the iPhone to an X from an 8 in the simulator to solve my problem.
Possible reasons:
view controller overrides touchesBegan(_:with:) method which don't call the super.
UIView userInteractionEnabled is set to false on that button via InterfaceBuilder or programmatically
button is below a view which has userInteractionEnabled set to false

SFSafariViewController : Overlay is not appearing sometimes

I am using SFSafariViewController, and as i did not wanted to show the status bar of safari and i have hidden it by adding a overlay. I have my own button on the overlay and its working fine. The safari status bar is hidden and it shows my overlay but somehow sometimes the overlay disappears and the safari status bar is visible. I doubt that its because the link is being reloaded automatically but how? I am only calling it once. I am not sure why overlay is disappear for sometimes only. below is my code -
self.navigationController?.presentViewController(sfController!, animated: true, completion: {
let bounds = UIScreen.mainScreen().bounds
let overlay = UIView(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: 65))
overlay.backgroundColor = UIColor(netHex: 0x275E37)
let completedBtn = UIButton()
let favBtn = UIButton()
let image = UIImage(named: "home.png") as UIImage?
let homeBtn = UIButton(type: UIButtonType.Custom) as UIButton
homeBtn.frame = CGRectMake(20, 25, 35, 35)
homeBtn.setImage(image, forState: .Normal)
homeBtn.addTarget(self, action: #selector(DetailsViewController.HomeBtnAction), forControlEvents:.TouchUpInside)
completedBtn.setTitle("Mark as Completed",forState: .Normal)
completedBtn.titleLabel!.font = UIFont(name: "FidelitySans-Regular", size: 20)
completedBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
completedBtn.frame = CGRectMake((bounds.size.width - 210), 25, 200, 35)
completedBtn.backgroundColor = UIColor(netHex: 0x6F9824)
completedBtn.addTarget(self, action: #selector(DetailsViewController.CompletedAction), forControlEvents: UIControlEvents.TouchUpInside)
favBtn.setTitle("Save as Favorite", forState: .Normal)
favBtn.titleLabel!.font = UIFont(name: "FidelitySans-Regular", size: 20)
favBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
favBtn.frame = CGRectMake((bounds.size.width - 420), 25, 200, 35)
favBtn.backgroundColor = UIColor(netHex: 0x6F9824)
favBtn.addTarget(self, action: #selector(DetailsViewController.FavoriteAction), forControlEvents: UIControlEvents.TouchUpInside)
overlay.addSubview(favBtn)
overlay.addSubview(completedBtn)
overlay.addSubview(homeBtn)
self.sfController!.view.addSubview(overlay)
})
Any help will be much appreciated as i am stuck in this issue from long time and cant fine solution. Thanks!
I got solution by adding overlay in the safariViewController delegate method didCompleteInitialLoad. So a indicator with transparent dark background until the didCompleteInitialLoad method is not called, once the URL is fully loaded didCompleteInitialLoad will be called and not add the overlay to safariviewcontroller in this method. like below code so that its always on the top of the stack.
self.sfController?.view.insertSubview(overlay, atIndex: (self.sfController?.view.subviews.count)!)
What you are trying to do is not allowed. SFSafariViewController should always be presented as it is.
Instead, you could use WKWebView to build an in-app browser and add custom controls.

Swift adding multiple images to a single UIButton

In my app, I would like to add multiple images on a single UIButton.
This is what I have now:
NextButton = UIButton(frame: CGRect(x: width*0, y: height*0.55, width: width*0.95, height: height*0.05))
NextButton.addTarget(self, action: #selector(OnNextPressed), for: .touchUpInside)
NextButton.isExclusiveTouch = true
NextButton.setTitle("ButtonText", for: UIControlState())
NextButton.setTitleColor(UIColor(red:0, green:0, blue:0, alpha:1.0), for: UIControlState())
NextButton.setBackgroundImage(UIImage(named: "ButtonOutline"), for: .normal)
view.addSubview(NextButton)
I would like a 2nd image as an arrow pointing to the next page.
thanks in advance :)
the rest of the button works but I don't know how to make the arrow appear
I fixed the problem here is the new code:
let screenSize: CGRect = UIScreen.main.bounds
let width = screenSize.width
let height = screenSize.height
NextButton = UIButton(frame: CGRect(x: width*0, y: height*0.55, width: width*0.95, height: height*0.05))
NextButton.addTarget(self, action: #selector(OnNextPressed), for: .touchUpInside)
NextButton.isExclusiveTouch = true
NextButton.setTitle("ButtonText", for: UIControlState())
NextButton.setTitleColor(UIColor(red:0, green:0, blue:0, alpha:1.0), for: UIControlState())
NextButton.setBackgroundImage(UIImage(named: "ButtonOutline"), for: .normal)
NextButton.setImage(UIImage(named: "arrow_next"), for: UIControlState())
NextButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, width*0.7)
view.addSubview(NextButton)
by simply adding setImage to the code i could create a second image on top of the BackgroundImage. then by suing imageEdgeInsets i aligned the image on the button.

Can't click custom added UIBarButtonItem

I've got this transparent Navigation bar, and I can add a custom icon (code below). However it does not seem to respond to clicks, anyone have any idea why this could be caused?
I'm adding this inside a UIViewController which is inside a UINavigationController.
var button: UIButton = UIButton()
button.setImage(UIImage(named: "customBack"), forState: .Normal)
button.frame = CGRectMake(0, 0, 40, 40)
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
button.targetForAction("doAction:", withSender: button)
button.backgroundColor = UIColor.brownColor()
var leftItem:UIBarButtonItem = UIBarButtonItem()
leftItem.customView = button
self.navigationItem.leftBarButtonItem = leftItem
self.navigationItem.leftBarButtonItem!.action = "doAction:"
self.navigationController!.navigationItem.leftBarButtonItem = leftItem
I know I shouldn't be adding back items myself but in this exact case it is needed. The icon is added on the fly not before loading the view or w/e.
To try it...
var ysBackButton:UIButton = UIButton(frame:CGRectMake(0, 0, kNavBtnSize, kNavBtnSize))
ysBackButton.setImage(UIImage(named: kBack), forState: UIControlState.Normal)
ysBackButton.addTarget(self, action: Selector("backButtonAction"), forControlEvents:UIControlEvents.TouchUpInside)
var leftBarButton:UIBarButtonItem = UIBarButtonItem(customView: ysBackButton)
self.navigationItem.leftBarButtonItem = leftBarButton
Always just after posting I find out:
I was using
button.targetForAction("doAction:", withSender: button)
Whilst I should be doing:
button.addTarget(self, action: "doAction:", forControlEvents: .TouchUpInside)

Resources