Navigation Left bar button item with back button [duplicate] - swift

This question already has answers here:
How do I add a left bar button without overriding the natural back button?
(2 answers)
Closed 6 years ago.
I want to add left bar button item in navaigationBar.
I have back button sign (do not have any text) in left side and when I add leftBarButtonItem then I can not see the back button.
How can I solve this problem.
This is my code:
let phonePhoto = UIButton()
phonePhoto.setImage(UIImage(named: "navigationPhone"), for: UIControlState())
phonePhoto.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
phonePhoto.addTarget(self, action: #selector(userEdit), for: .touchUpInside)
let userPhone = UIBarButtonItem()
userPhone.customView = phonePhoto
self.navigationItem.leftBarButtonItem = userPhone

The presence of custom left bar button items causes the back button to be removed in favor of the custom items. Set navigationItem property leftItemsSupplementBackButton to true will display back button also with your custom left bar item.
self.navigationItem.leftItemsSupplementBackButton = true
Check Apple Documentation on leftItemsSupplementBackButton for more details.

Related

Label is not affected by default darkening (highlight) while pressing a button in Swift

Background: I am creating a button with an image and a label. I add the label to the button with button.addSubview(labelName) and then I add the button to the main view with self.view.addsubview(buttonName). I am creating all views programatically and with UIKit. I do not use SwiftUI or Storyboard.
Problem: When pressing the button and holding the press, by default the button image automatically gets darker and loses color (I do not know what this animation or action is called, but I guess it's some kind of default button press/click action). The gif below shows this animation.
The problem is, only the background image of the button is affected. The label and the label background, that I added to the button, are not effected by this dim animation.
What I want to achieve: I want the label and the label background to get the same dim effect, when the button is pressed. I don't like the way the label stands out with its bright colors while the button is pressed. Do I need to recognize the click and add some kind of dim animation to all childviews of the button manually or should I avoid adding labels to buttons like I did in the code below?
Simplified code for testing:
func doStuff(){
let testbutton = UIButton(frame: CGRect(x: self.view.frame.width/4, y: 100, width: 200, height: 130))
let testImg = UIImage(named: "rand.jpeg")
testbutton.setImage(testImg, for: .normal)
testbutton.addTarget(self, action: #selector(otherStuff), for: .touchUpInside)
let label = UILabel(frame: CGRect(x: testbutton.bounds.origin.x+20, y: testbutton.bounds.origin.y+20, width: 150, height: 50))
label.text = "NOT EFFECTED"
label.backgroundColor = .red
testbutton.addSubview(label)
self.view.addSubview(testbutton)
}

Swift NavBar Hide NavBarItem

Inside of my view controller I have a navBar with an item on the left side and right side. I also have 2 buttons inside of the view controller. I have it so each button does different functionality. I also have the swift file connected to the view.
My issue is that when I do either of the code below inside of my button actions the NavBar Items do not change. I don't know how to remove and add the item back, I also dont understand what Im doing wrong.
self.navigationItem.rightBarButtonItem?.isEnabled = false
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.clear
self.navigationItem.rightBarButtonItem?.isEnabled = true
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.red
Connect Navigation Bar from storyboard to View Controller Class
#IBOutlet var navBar: UINavigationBar!
This will hide Button
navBar.topItem?.rightBarButtonItem?.isEnabled = false
navBar.topItem?.rightBarButtonItem?.tintColor = UIColor.clear
This will show Button
navBar.topItem?.rightBarButtonItem?.isEnabled = true
navBar.topItem?.rightBarButtonItem?.tintColor = UIColor.red
Sorry, i can't make comment due to low reputetion. Which IDE are you using?
The answer of #dharmesh works, if you create the button programmatically:
let btn = UIButton(type: .system)
btn.setTitle("Indietro", for: btn.state)
btn.addTarget(self, action: #selector(annulla), for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
If you use the one from the IDE it depends on the IDE itself to store the values to hide or change it

Making a UITabBarButton trigger a modal segue

I want to mimic Instagram's tabbar functionality, where the middle option acts as a button that triggers a modal slide up segue instead of switching views like a normal tabbar button.
I don't know the correct way of doing this, but I'm trying to place a button directly over the middle tabbar button, but when pressing it it triggers the tabbar button instead of the normal button.
let count = CGFloat(tabBar.items!.count)
let itemSize = CGSize(width: tabBar.frame.size.width / count, height: tabBar.frame.height)
for (index, _) in tabBar.items!.enumerate() {
if index == 2 {
let xPosition = itemSize.width * CGFloat(index)
let backgroundButton = UIButton(frame: CGRect.init(x: xPosition, y: 0, width: itemSize.width, height: itemSize.height))
backgroundButton.backgroundColor = UIColor.redColor()
backgroundButton.addTarget(self, action: #selector(MainTabBarController.testButton), forControlEvents: .TouchUpInside)
//testButton just triggers a print statement for now
tabBar.insertSubview(backgroundButton, atIndex: 1)
}
}
When I press the middle button, it just triggers the normal view switching like with any other tabbar button, and does not print anything out meaning the overlayed button is never trigger.
Is there somewhere I went wrong, or better yet is there a better way of doing this? Prehaps making the tabbar button itself trigger a modal segue instead of the typical tabbar button behavior?

Swift - Automatically adding a button to the right of the button just added

I have a UIScrollView set up, and am using addSubView to add 5 buttons to it. Here is the code inside my viewDidLoad.
let codedButton:UIButton = UIButton(frame: CGRectMake(0, 0, 100, 50))
codedButton.backgroundColor = UIColor.redColor()
codedButton.setTitle("Button 1", forState: UIControlState.Normal)
codedButton.addTarget(self, action: #selector(ViewController.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
codedButton.tag = 1
self.buttonScrollView.addSubview(codedButton)
let codedButton2:UIButton = UIButton(frame: CGRectMake(110, 0, 100, 50))
codedButton2.backgroundColor = UIColor.redColor()
codedButton2.setTitle("Button 2", forState: UIControlState.Normal)
codedButton2.addTarget(self, action: #selector(ViewController.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
codedButton2.tag = 2
self.buttonScrollView.addSubview(codedButton2)
(all the way to 5.)
The problem that I need help with is... Ultimately, these buttons will be dynamically displayed. Not just a set amount of 5.
How do I adjust the X value of the CGRectMake to automatically "float" new buttons to the right of the button that was just created? In my code above, I am explicitly stating where the X is, but that won't work properly once these buttons become dynamically created in code.
EDIT
To clarify my question above:
I have attached an image. Note how the buttons always stack horizontally / next to each other, regardless of if there's 2 3 4 or 5. These buttons will be dynamically shown. Therefore, with my code attempt I have provided above, it will not work because it is explicitly setting where to place the buttons on the X axis.
I need to modify my code to perform like the image below.
I see that you are doing this programmatically. I can't help with the code, but stack views in the storyboard are great. I have a feature in my app where I have 4 buttons, but the user may only see, say, 2 of the 4 buttons.
1) Add the buttons horizontally into the storyboard (They don't need to be perfectly aligned, just enough so that Xcode can recognize it's horizontal rather than vertical.
2) Select all 5 buttons
3) Click on the stack view button in the lower right-hand corner of Xcode (The button with 4 horizontal lines with and an arrow pointing down.
4) In the attributes inspector, choose horizontal as the axis, alignment as center, fill proportionately, play with the spacing, and choose scale to fill as the mode.
5) In the Swift file, you can add or hide the buttons using button.hidden = true / false
NOTE: Keep in mind that stack views are only available with iOS 9 when I last checked
Below is an example of the loop you can add to your current implementation of one button to add as many buttons as you would like, and in this example the number of buttons is dependent on the number of button names that you add to the array called "buttonList". For each iteration of the loop it will name the button from the text in the ButtonList and set the tag to its index in the array. It will then calculate the x coordinate of your frame, and in this example I made each button 100px wide, and have a 10px space in between, starting at x coordinate location 0. If you wanted to add 10px space between the first button and the leading edge of the view just increment the index when calculating the x coordinate with:
let xCoord = CGFloat(index*buttonWidth + (index+1)*buttonSpace)
You will also need to increase the size of your scroll view to fit buttons as more are added, this is the calculation I used for setting my scroll view width (Increment the buttonSpace width by 1 if adding a leading space to your scroll view) : buttonScrollView.contentSize = CGSizeMake(CGFloat((ButtonList.count * buttonWidth) + ((ButtonList.count + 1) * buttonSpace)), 50)
var ButtonList = ["Button 1", "Button 2", "Button 3"]
let buttonWidth = 100
let buttonSpace = 10
for (index,ButtonText) in ButtonList.enumerate(){
//calculate the x coordinate of each button
let xCoord = CGFloat(index*buttonWidth + index*buttonSpace)
let codedButton:UIButton = UIButton(frame: CGRectMake(xCoord, 0, 100, 50))
codedButton.backgroundColor = UIColor.redColor()
codedButton.setTitle(ButtonText, forState: UIControlState.Normal)
codedButton.addTarget(self, action: #selector(ViewController.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
codedButton.tag = index
self.buttonScrollView.addSubview(codedButton)
}
I do not know exactly if that is what you're asking for.. if not, leave a comment.
Create a counter and name your buttons..
button.name = "name\(counter)"
Get the position of your last button using..
let position = self.childNodeWithName("name\(counter)")?.position
Set the position of your new button..
newButton.position = CGPointMake(position.x + newButton.frame.size.width/2 + oldButton.frame.size.width/2, position.y)
.. your new button is now set right to your last button added

How to enable the userinteraction only in the barbutton item of the navigation bar?

I have an application which is having the new Facebook App like UI. In that, a side menu controller is there. It has a button on the navigation bar while clicking on to it will give u the menu. But my problem is in the sample I am using the navigation bar is also can be movable, I fix this by setting userinteractionenabled=no in the navigation bar. But I need to enable the userinteraction only in the buttons in the navigation bar. If I do like what I did the whole navigation bar is became not clickable. Can anybody help me in achieving this?
If you have custom button then use this
UIBarButtonItem *button = self.navigationItem.rightBarButtonItem;
button.enabled = NO;
This is the code I use. It doesn't quite enable user interaction, but I figured out how to set the title white then when clicked change colors. (look at setTitleColor lines
override func viewDidLoad() {
super.viewDidLoad()
let handleButton = UIButton.init(type: .custom)
button.setTitle("Register", for: .normal)
button.layer.backgroundColor = CGColor.init(gray: 0.0, alpha: 0.0)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.white.cgColor
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
if let button = self.navigationItem.rightBarButtonItem?.customView {
button.frame = CGRect(x:0, y:0, width:80, height:34)
}