The add button is below the navigation panel - ios

Can someone help me with how I could inline the add button with the navigation panel? Its shown below the panel.
This is the code I used:
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
self.navigationItem.rightBarButtonItem = add

Related

SWRevealViewcontroller Code Query for implementing navigation controller Bar Button

I was trying to implement the bar button with SWreveal ViewController and instead of implementing the slider button "Open" For all views.I made one base class BaseController and from that I inherited the viewcontroller for which i wish to have slider but my below code works for the pan gesture but code 2 doesn't not for the button.Can Someone tell me why?
Code1:(Working Code)
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "Sider", style: .done, target: self, action: nil ) self.navigationItem.leftBarButtonItem?.target = self.revealViewController() self.navigationItem.leftBarButtonItem?.action = Selector("revealToggle:")
Code2:(Not Working)
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "Sider", style: .done, target: self, action: Selector("revealToggle:") )
Code1 works but Code2 doesn't work Can someone tell me why.
In your Code2 check Selector("revealToggle:") is not your class method so you have to select target: SWrevealViewController in code2
You can write code2 like below:
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "Sider", style: .done, target: self.revealViewController(), action: Selector("revealToggle:") )

How to customize UItoolbar

Twitter Registration Page
Hi everyone I have some cushions about UItoolBar on swift 3/4
I'm trying to make an singIn design like twitter,
1) i create toolbar with code;
let toolBar = UIToolbar()
toolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked))
toolBar.setItems([flexibleSpace, doneButton], animated: false)
kullaniciAdi.inputAccessoryView = toolBar
eposta.inputAccessoryView = toolBar
sifre.inputAccessoryView = toolBar
but I can't add custom buttons on that. Like changing the "Done" text in my language.
2) So I created manually using Storyboard a toolBar with inside a button and Flexible Space. But ı need to "attach" that toolbar to the keyboard, so when i start to edit the textfield the toolbar will automatically show up. NOTE: it will be fantastic if the toolbar will show too when not-editing textField. Like that:
My app trying too attach toolbar
THANK YOU ALL
In Code
let sonrakiButton = UIBarButtonItem(title: "Sonraki", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.sendDoneBtnAction))
For more info, read this link

Center bar buttons in toolbar in Xcode

I have two buttons inside of a toolbar that is positioned at the bottom of the screen. I would like to center those buttons within the toolbar. Everything I have found is focused on centering buttons within a nav bar and I am unsure of how to approach this issue. What am I missing?
This is the UI I am attempting to emulate:
Adding a flexible space to the left and right of the buttons centered the buttons inside of the toolbar. Thanks to Midhun MP for the tip.
Thks #Andrew, it works
but for someone who wants it from code here is the example in Swift 5.0
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setToolbarHidden(false, animated: true)
let spaceItemLeft = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let nextItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(nextTapped))
let spaceItemRight = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [spaceItemLeft, nextItem, spaceItemRight]
}
#objc func nextTapped() {
print("NEXT Tapped")
}

Swift 3.0 Adding a Right Button to Navigation Bar

I have added a navigation bar to the top of a view controller. I am trying to control whether a button is visible based a condition, but I am having trouble adding the button. So far I have,
var addButton: UIBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))
override func viewDidLoad() {
super.viewDidLoad()
let boool = true
if boool {
self.navigationItem.rightBarButtonItem = self.addButton
}
else {
self.navigationItem.rightBarButtonItem = nil
}
}
func addTapped(sender: AnyObject) {
print("hjxdbsdhjbv")
}
I believe it is not working properly because I have added a navigation bar into the VC, instead of using a navigation controller and working with the bar there. I was wondering if there was a way to work with this navigation bar.
It’s simple. Put this line of code to the viewDidLoad:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))
Updated for Swift 4 or later:
A custom function:
#objc func action(sender: UIBarButtonItem) {
// Function body goes here
}
(Custom) Right bar button item:
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))
(Custom) Left bar button item:
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))
Also you can add a system bar button items something like this: UIBarButtonItem.SystemItem Defines system-supplied images for bar button items: .add, .done, .cancel, .edit, .save, .compose, .reply, .organize and more.
(System) Right bar button item:
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))
(System) Left bar button item:
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))
let rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "EditImage"), style: .done, target: self, action: #selector(ViewController.call_Method))
self.navigationItem.rightBarButtonItem = rightBarButtonItem
You say you added a UINavigationBar to your view controller via storyboard, but looking at the code you provided there is no outlet connection to your navigation bar in IB.
In order to access self.navigationItem your view controller must be embedded in a UINavigationController or be part of a hierarchy which is. Unless you have a need for a custom navigation bar on an individual view controller, I suggest removing that from Interface Builder, then making sure either the view controller in question is embedded in a UINavigationController or it is being pushed onto the navigation stack from another controller which is embedded in a navigation controller and then you should see your UIBarButtonItem.
Firstly, you need to connect you navigation bar to an IBOutlet so that you can refer to it in your code. After that, this code should work:
let navigationItem = UINavigationItem(title: "Title")
navigationItem.rightBarButtonItem = self.addButton
navigationItem.hidesBackButton = true
self.navigationBar.pushItem(navigationItem, animated: false)
Swift 4.2;
Add viewController
override func viewDidLoad() {
super.viewDidLoad()
self.addNavigationBarButton(imageName: "ic_back", direction:.left)
}
Add Class your API or Utility Class
public func addNavigationBarButton(imageName:String,direction:direction){
var image = UIImage(named: imageName)
image = image?.withRenderingMode(.alwaysOriginal)
switch direction {
case .left:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
case .right:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
}
}
#objc public func goBack() {
self.navigationController?.popViewController(animated: true)
}
public enum direction {
case right
case left
}
tested in Xcode 10.2, swift 5.0;
First, I have have embedded my ViewController in UINavigationController in IB.
Then in ViewDidLoad include these lines
self.title = "orange"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeLayout)).
Note - accessing title, or adding button through navigation controller did not work. For example : setting title - Self.navigationcontroller.navigationItem.title did not work ,

How to add right button in the navigation bar?

I have a question to add a right button in navigation bar..
I have two views: View A and View B
I add a navigation bar to view A, after I used self.navigationController!.pushViewController to show view B.
That show a back button in the left of navigation bar of view B automatic, it is good. but now I want to add a button in the right of navigation bar of view B..
I have tried some answers, but it doesn't work...
I have tried answers likes :
1) https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar
2)http://blog.apoorvmote.com/add-multiple-bar-button-item-navigation-bar/?lang=fr
Could you help me, thank you !
The Swift version of Vahan Babayan's answer, as you seem to use this language, is:
let rightButtonItem = UIBarButtonItem.init(
title: "Title",
style: .Done,
target: self,
action: "rightButtonAction:"
)
self.navigationItem.rightBarButtonItem = rightButtonItem
The following method will be called on self:
func rightButtonAction(sender: UIBarButtonItem)
Note that all this can be set graphically using a Storyboard, by dragging a Bar Button Item to your Navigation Item and right-clicking to set a target-action.
A small update since Swift 3 and 4 are out: the compiler can now check selector names, preventing typos when setting up target-action programatically. So one should really use:
let rightButtonItem = UIBarButtonItem.init(
title: "Title",
style: .Done,
target: self,
action: #selector(rightButtonAction(sender:))
)
You can add the following code to your B controller's viewDidLoad method.
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Title"
style:UIBarButtonItemStyleDone
target:self
action:#selector(rightButtonAction:)];
self.navigationItem.rightBarButtonItem = rightButtonItem;
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:#"Right Button" style:UIBarButtonItemStyleDone target:self action:#selector(rightBtnClick)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void)rightBtnClick{
// code for right Button
}
Add below code in viewDidLoad Method
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView:)];
self.navigationItem.rightBarButtonItem = flipButton;
This adds a button to the right hand side with the title Flip, which calls the method:
-(IBAction)flipView
Swift code to add a navigation bar button item:
Method 1 (When you wanna use bar button system item)
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
Method 2 (When you wanna give your own title to button)
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
From iOS 5 onwards, it allows you to add more than 1 button on either side of a navigation bar. Something like this:
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
let display = UIBarButtonItem(title: "Display", style: .plain, target: self, action: #selector(playTapped))
navigationItem.rightBarButtonItems = [add, display]
Try this code for Swift UI :
.navigationBarItems(
leading:
HStack {
Button("About") {
print("About tapped!")
}
Button("Call") {
print("Call tapped!")
}
},
trailing:
HStack {
Button("Settings") {
print("Settings tapped!")
}
Button("Contacts") {
print("Contacts tapped!")
}
}
)

Resources