Hey!
Having problem with my navigationBar and havn't found a solution anywhere. I have changed the titleView text to an image. Now I want it to be a clickable button with a segue to the starting page (a home button). How do I make it into a button (or item) and how do I create a segue from it. I suppose it has to be done in code since I havn't found any other means of doing it. If you have a solution, please be thorough - I'm a beginner 🙂
How to create a button and segue from titleView image
You could try the following :
- insert an UIView in place of titleView
- Change its class as UIControl
- yoyu can now attach an IBAction on mouse down event
You'll have to insert your imlage in the view.
Make titleView a UIButton with a Segue to Another Controller
- For demonstration, create a Single View Project
- Open Main.storyboard and Select the View Controller
- (Xcode Menu Selections) Editor > Embed in > Navigation Controller
- Drag a Button control from the Object Library; and, drop into the View Controller scene's icon dock (the set of icons just above the View Controller image on the storyboard). A new icon for the button will appear in the dock to the left of the Exit icon; and, an image of the new button will appear at the top of the View Controller's scene.
- Option-click (or two-finger-touch) the Navigation Item in the View Controller. You can do this on the visual display of the Navigation Item of the View Controller's scene in the storyboard canvas, or in the hierarchical display of storyboard objects to the left of the storyboard canvas. This will display the black-backgrounded "HUD", which lists the Outlets for the Navigation Item, and their current connections. Note that the titleView Outlet is not connected yet. Connect it now, by dragging from the open circle corresponding to the titleView Outlet in the HUD to the icon of the Button in the first View Controller's icon dock. The HUD will show that the connection was successful by filling in the circle, and listing "Button" as the object assigned to the titleView Outlet. Note that the drag operation is just a plain mouse drag--no other keys are used; and, if you do try to drag using (e.g.) the control or option keys as part of the drag, the HUD just fades away.
- Drag a View Controller from the Object Library; and, drop it in the storyboard. Just to the right of the original View Controller is a good position for it. You may want to set the backgroundColor of this View Controller's View to a different color from the first View Controller’s View, just to clearly show when the segue is working properly.
- Control-drag from the Button's icon in the dock of the first View Controller, to the View area of the second View Controller. A connecting line will display as you drag; and, when the View is the drop-target, the View will highlight. When you stop dragging, the black-background "HUD" will display the available segues. Select the segue type you want to use for this segue.The HUD should show under Triggered Segues, an action of View Controller Show (or whatever segue type you choose), with a filled-in circle
- At this point, a navigation area will show on the second View Controller's scene, with a "< Back" button.
- You can now build/run the app. "Button" will display instead of the title; and, touching the Button will segue to the second View Controller. Touch the "< Back" button to go back to the first View Controller.
That's it. No code required!
You can, of course, modify the attributes of the Button, e.g., by adding an image, etc.