In the app I'm making I have a side menu that I used SWRevealViewController template to make. I made my own animated button to be the hamburger menu button so when its pressed the side menu will open. The problem is I can't figure out how to connect my animated button to the SWRevealViewController.
Here's the button code I made.
Animated Button
self.button = HamburgerButton(frame: CGRectMake(0, 0, 30, 30))
self.button.addTarget(self, action: #selector(home.toggle(_:)), forControlEvents:.TouchUpInside)
let refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh,
target: self, action: #selector(home.buttonMethod))
navigationItem.leftBarButtonItem = button
and heres the button that was used for the SWRevealViewController
override func viewDidLoad() {
super.viewDidLoad()
if revealViewController() != nil {
menuButton.target = revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
Ive done a lot of research but just can't find out how to do it. I need the button I made, which is the first code, to be the one to access the SWRevealViewController and to open and close the side menu rather then the button, which is the second code, that came with the SWRevealViewController template. Any help will be Awesome!!
This is how I do it. You can adapt this to your needs.
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
singleTap.numberOfTapsRequired = 1
sideMenuButton.userInteractionEnabled = true
sideMenuButton.addGestureRecognizer(singleTap)
func tapDetected() {
self.revealViewController().revealToggle(self)
}
Related
I made a navigation bar with three buttons - Plans,Visit and Documents and every time a button is selected/clicked a view is added as a child into a container view (Code for the other buttons are similar)
#IBAction func planAction(_ sender: UIButton) {
let newPlan = sender.frame.origin.x
scrollView.subviews.forEach{$0.removeFromSuperview()}
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextController = storyBoard.instantiateViewController(withIdentifier: "PlansViewController2") as! PlansViewController2
addChildView(viewController: nextController, in: scrollView)
self.movingView.frame.origin.x = newPlan
}
Now I want to add a tool bar item that only shows up when Plans or Documents is selected and disappears when Visit is selected.
At the moment I add my tool bar item in the viewDidLoad method:
self.toolbarItems = [UIBarButtonItem(title: "Kategorienliste", style: .plain, target: self, action: #selector(handleCategoryListTap(sender:)))]
How can I add a toolbarItem when a certain button is selected and remove it when another button is selected? Thanks in advance!
You can create your UIBarButtonItem with a custom view like:
let button = UIButton()
let barButtonItem = UIBarButtonItem(customView: button)
self.toolbarItems = [barButtonItem]
to hide / show you can access the button as the following:
let barButtonItem = self.toolbarItems.first
barButtonItem.customView?.isHidden = true
Hope it helps.
I have a project with a UITabBarController and a couple of views. Sort of like this:
In this project, the tab bar controller is called by tapping the Tab button on the UIViewController. I would like to get rid of the back button with "Title", and replace it with an "X" icon. When tapped, the "X" icon would dismiss the UITabBarController and return to the caller. I do this all of the time on UINavigationController using a UINavigationItem, but that does not appear to work in this situation. I drag the UINavigationItem to the view and it allows it, but it does not show up on the view and any UIBarButtonItem that I drag and drop on it do not appear.
Is there a way to actually do this? I'd even be ok with leaving the existing back button as it is and just getting rid of "Title"
I figured it out right after posting the question. Just a bit more research is all it took.
To fix this, add the following to the UITabBarController code.
override func viewDidLoad() {
super.viewDidLoad()
let buttonImage = UIImage(named: "back")
let leftButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(dismissTabBar))
leftButton.tintColor = UIColor.black
self.navigationItem.leftBarButtonItem = leftButton
}
#IBAction func dismissTabBar() {
self.navigationController?.popToRootViewController(animated: true)
}
This gives me a black back button with my image. When I tap it, it brings me back to the calling 'UIViewController.
I've got some fairly simple code, and am trying to figure out why the uibarbuttonitem isn't triggering the selector:
TableView's viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
self.clearsSelectionOnViewWillAppear = false
// this line adds a button to top right
let addNewChecklistItemButton = UIBarButtonItem(barButtonSystemItem: .add,
target: nil,
action: #selector(self.addNewChecklistItemToDataModel))
self.navigationItem.rightBarButtonItem = addNewChecklistItemButton
}
Method called:
#objc func addNewChecklistItemToDataModel() {
print("adding new item...")
<...>
}
The button is being added (was not visible before I put this code in) and when I press it the button click animates but my console doesn't show the printed text.
After reading UIBarButtonItem selector not working, I'm wondering if the fact that my UITableViewController is embedded in both a UITabViewController and UINavigationViewController is effecting the button's scope? If not, anyone see something I'm missing?
Target shouldn't be nil
let addNewChecklistItemButton = UIBarButtonItem(barButtonSystemItem: .add,
target: self,
action: #selector(self.addNewChecklistItemToDataModel))
I have set auto-layout for the label so it should be centered right above my button, however when I click on the button the label goes up in the left corner. I'm a totally new beginner to coding, so bare in mind I might have just missed something very simple? I would guess in my code I need some kind of way to tell the position of the label to be after the click?
This is my code for the button action:
nextStagetwo.textAlignment = NSTextAlignment.Center
nextStagetwo.userInteractionEnabled = true
nextStagetwo.text = "Go to next stage"
self.view.addSubview(nextStagetwo)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
nextStagetwo.addGestureRecognizer(gestureRecognizer)
}
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc: AnyObject! = storyboard.instantiateViewControllerWithIdentifier("stagethree")
self.presentViewController(vc as! UIViewController, animated: true, completion: nil)
}
Run your application in Xcode 7 and select the debugger view and then the debugger toolbar button with tooltip, "Debug View Hierarchy" (third button from right in the debugger toolbar). That will show you what auto layout is computing for each item on the screen. You can peel off layers as needed in this view. Compare those values with your auto layout constraints for that item and its containers.
Also, make sure you don't have any auto layout warnings in the Xcode Interface Builder for your storyboard.
I added this to the code, which was what was missing in my code:
let nextStagetwo = UILabel(frame: CGRectMake(250, 250, 250, 250))
nextStagetwo.center = CGPointMake(210, 200)
I'm trying to get my side menu (swreveal) to show when clicking a UIImageView with a tap gesture or button. I've got the image view and tap all set up and the tap gesture linked to an action. Getting this functionality is easy when using a bar button item but I can't seem to get it to function when using a plain old button or image view.
Here's how I do it for a bar button item:
barButtonItem!.target = self.revealViewController()
barButtonItem!.action = Selector("revealToggle:")
This obviously doesn't work for buttons or ImageViews because they don't have a member of target or action. Would really appreciate some assistance on this one!
To add target to UIButton you can use this:
...
let button = UIButton()
button.addTarget(self, action: "someAction:", forControlEvents: .TouchUpInside)
...
func someAction(sender: UIButton!) {
...
}
For adding action to press on UIImageView you can use UITapGestureRecognizer like this:
...
let tap = UITapGestureRecognizer(target: self, action: Selector("someAction"))
tap.numberOfTapsRequired = 1
image.userInteractionEnabled = true
image.addGestureRecognizer(tap)
...
func someAction() {
...
}