How to add multiple buttons to navigation bar without UINavigationController? - ios

to add a multiple button to viewController inside UINavigationController,
I just this line:
self.navigationItem.rightBarButtonItems = [my button]
but i din't succeed to add that to a view wihout UINavigationController:
//create new navigation item. and add it to the nvigation bar
customNavigationItem = UINavigationItem()
/* now we need to add the menu + search to the navigation item */
//create search button
let searchButton = UIButton();
searchButton.frame = CGRectMake(0, 0, 50.0, 50.0);
searchButton.backgroundColor = UIColor.redColor();
searchButton.titleLabel!.font = UIFont.iconmoonFont(14.0)
searchButton.titleLabel?.textColor = UIColor.whiteColor()
searchButton.titleLabel!.text = IconsConstants.SEARCH;//SEARCH ICON TEXT
searchButton.backgroundColor = UIColor.clearColor();
searchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center;
searchButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center;
let searchUIbarBtn = UIBarButtonItem.init(customView: searchButton)
//create menu button
let menuButton = UIButton();
menuButton.frame = CGRectMake(0, 0, 40.0, 40.0);
menuButton.titleLabel!.font = UIFont.iconmoonFont(14.0)
menuButton.backgroundColor = UIColor.whiteColor()
menuButton.titleLabel!.text = IconsConstants.MENU;//SEARCH ICON TEXT
menuButton.backgroundColor = UIColor.clearColor();
menuButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center;
menuButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center;
let menuUIbarBtn = UIBarButtonItem.init(customView: menuButton)
//add button to NavigationItem
customNavigationItem.rightBarButtonItems = [menuUIbarBtn,searchUIbarBtn];
/* add navigation bar */
navigationBar = UINavigationBar();
navigationBar.frame = CGRectMake(0, statusBarHeight, self.view.frame.size.width, 44);
//navigationBar.translucent = false
navigationBar.barTintColor = UIColor.statusBarColor()
navigationBar.items = [customNavigationItem]
self.view.addSubview(navigationBar)
i can't understand why i got a bar without any button ?

Related

How can I turn my UINavigationBar into UINavigationController (for use with this library)?

I am using this MRProgress Library. It takes aUINavigationController as an argument. However, in my ViewController (which is presented as a modal), I create a UINavigationBar by myself.
// Create the navigation bar
navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44 + statusBarHeight()))
navigationBar.backgroundColor = UIColor.whiteColor()
navigationBar.delegate = self;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "New Conversation"
navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName: COLORS.logo0,
NSFontAttributeName: FONTS.title
]
//Back Button
let backButton = UIButton(type: .Custom)
backButton.addTarget(self, action: #selector(CreateSessionViewController.backButtonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
backButton.frame = CGRectMake(0, 0, 20, 20)
let cross = UIImage(named: "cross.png")
backButton.tintColor = UIColor(hex: 0xcccccc)
backButton.setImage(cross?.imageWithRenderingMode(.AlwaysTemplate), forState: UIControlState.Normal)
backButtonBar = UIBarButtonItem(customView: backButton)
navigationItem.leftBarButtonItems = [backButtonBar]
//Add nav bar
navigationBar.items = [navigationItem]
self.view.addSubview(navigationBar)
How can I pass this navigation bar to the library as an argument? This is what the library supports:
MRNavigationBarProgressView(forNavigationController: UINavigationController!)
If it makes sense, you could present modally a UINavigationController that wraps this ViewController.

Adding a logo in Navigation Bar that is left aligned

I have been stumped on how to get an image in a NavigationBar that is left aligned. I understand how to replace the title in the center.
Basically, I have a view controller embedded in a Navigation Bar Controller. In that view controller, I have a search form in the middle and a bar button on the right and I am trying to fit in a logo that is an image to the left side.
Below is the what I understand how to put an image but how do I get it left aligned?
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "imageName")
imageView.image = image
navigationItem.titleView = imageView
You can assign an array of items to the leftBarButtonItems property of the navigationBar. You should add an array because there is usually no space between the left side and the logo, and this doesn't look great. Here is an example:
func setUpUI() {
let logoImage = UIImage.init(named: "logoImage")
let logoImageView = UIImageView.init(image: logoImage)
logoImageView.frame = CGRectMake(-40, 0, 150, 25)
logoImageView.contentMode = .ScaleAspectFit
let imageItem = UIBarButtonItem.init(customView: logoImageView)
let negativeSpacer = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
negativeSpacer.width = -25
navigationItem.leftBarButtonItems = [negativeSpacer, imageItem]
}
The negative spacer to the left of it will push it over a bit, and you can adjust it from there.
For iOS 11 we need to add constraints to the image view of the BarButtomItem
func addLeftBarIcon(named:String) {
let logoImage = UIImage.init(named: named)
let logoImageView = UIImageView.init(image: logoImage)
logoImageView.frame = CGRect(x:0.0,y:0.0, width:60,height:25.0)
logoImageView.contentMode = .scaleAspectFit
let imageItem = UIBarButtonItem.init(customView: logoImageView)
let widthConstraint = logoImageView.widthAnchor.constraint(equalToConstant: 60)
let heightConstraint = logoImageView.heightAnchor.constraint(equalToConstant: 25)
heightConstraint.isActive = true
widthConstraint.isActive = true
navigationItem.leftBarButtonItem = imageItem
}
All the best
Per #Warrior's answer,
For Objective-C, iOS11+ and relative layout for the logo.
// create Youtube icon imageView
UIImageView *youtubeImageView = [[UIImageView alloc] init];
youtubeImageView.translatesAutoresizingMaskIntoConstraints = false;
youtubeImageView.contentMode = UIViewContentModeScaleAspectFit;
youtubeImageView.image = [UIImage imageNamed:#"youtube_icon"];
UIBarButtonItem *imageItem = [[UIBarButtonItem alloc] initWithCustomView:youtubeImageView];
// set constraints
float width = self.view.frame.size.width * 0.3;
float height = width * 178 / 794; // width * 1/ratio
[youtubeImageView.widthAnchor constraintEqualToConstant:width].active = true;
[youtubeImageView.heightAnchor constraintEqualToConstant:height].active = true;
self.navigationItem.leftBarButtonItem = imageItem;
Create a BarButton item with Image.
Assign it as Left Bar Button Item to Navigation Bar as follows.
Objective C
UIBarButtonItem *logo = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:"imageName"] style:UIBarButtonItemStylePlain target:self action:#selector(methodName)];
self.navigationItem.leftBarButtonItem = logo;
SWIFT
let logo = UIBarButtonItem(image: UIImage (named: "imageName"), style: UIBarButtonItemStyle.plain, target: self, action: methodName)
self.navigationItem.leftBarButtonItem = logo
Hope it helps..

How to add the UISegmentedControl in UINavigationBar?

I have tried to add the UISegmentedControl to the bottom of UINavigationBar with title. But i cannot add it and I cannot add UISegmentedControl in the tableView.headerView,because i need the search bar in the tableView.headerView, so there is just one solution that i need to add UISegmentedControl to the bottom of UINavigationBar. Anyone have another idea that i can solve this situation?
Here is the code that I have tried(with Swift):
class searchingViewController: UITableViewController{
override func viewDidLoad() {
var items: [AnyObject] = ["Searching Method A", "Searching Method B"]
var searchSC:UISegmentedControl!
searchSC.frame = CGRectMake(0, 0, frame.width - 20, 30)
searchSC.selectedSegmentIndex = 1
searchSC.backgroundColor = UIColor(white: 1, alpha: 1)
searchSC.layer.cornerRadius = 5.0
navigateUIToolBar = UIToolbar(frame: CGRectMake(frame.minX + 10, ios7_8Info.getStatusBarHeight()+self.navigationController!.navigationBar.frame.height+frame.minY+(21/2),
frame.width - 20, 30))
navigateUIToolBar.addSubview(searchSC)
self.navigationController?.navigationBar.addSubview(navigateUIToolBar)
}
}
To add segment control in UINavigationBar in Swift 5
let segment: UISegmentedControl = UISegmentedControl(items: ["First", "Second"])
segment.sizeToFit()
if #available(iOS 13.0, *) {
segment.selectedSegmentTintColor = UIColor.red
} else {
segment.tintColor = UIColor.red
}
segment.selectedSegmentIndex = 0
segment.setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "ProximaNova-Light", size: 15)!], for: .normal)
self.navigationItem.titleView = segment
To put it in the navigationBar has a right left and title view for such things... accessed using....
self.navigationItem.titleView = mySegmentedControl
for future reference....
self.navigationItem.rightBarButtonItem
self.navigationItem.leftBarButtonItems // for adding an Array of items
To add it below the navigation view but have it static.. add it to the viewController.view... Are you using a UITableViewController? If so maybe switch to a UIViewController and add a tableView then your toolbarview as subviews to that self.view.
I think you are looking for this.
let searchVC = self.storyboard?.instantiateViewController(withIdentifier:"idofcontroller")
let searchController = UISearchController(searchResultsController: searchVC)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["News", "Photos", "Videos"]
searchController.searchBar.delegate = self
This is how I do it in Objective C (sorry, I haven't learned Swift yet):
- (void)viewDidLoad
{
[super viewDidLoad];
self.viewControllers = [self segmentViewControllers];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems: [self.segmentedControl addTarget: self
action: #selector(segmentClicked:)
forControlEvents: UIControlEventValueChanged];
CGFloat topOffset = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
self.toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, topOffset, self.navigationController.navigationBar.frame.size.width, kDefaultViewHeight)];
self.toolbar.delegate = self;
self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
self.toolbar.backgroundColor = [UIColor whiteColor];
self.toolbar.clipsToBounds = YES;
UIBarButtonItem *segmentedControlItem = [[UIBarButtonItem alloc] initWithCustomView: self.segmentedControl];
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target: nil
action: nil];
[self.toolbar setItems: #[flexibleItem, segmentedControlItem, flexibleItem] animated: YES];
[self.navigationController.view addSubview: self.toolbar];
self.segmentedControl.selectedSegmentIndex = 0;
}
And in UITableViewController (one of the segmentViewControllers):
self.tableView.contentInset = UIEdgeInsetsMake(topOffset, 0, 0, 0);
CGRect currentFrame = self.tableView.frame;
[self.tableView setFrame: CGRectMake(currentFrame.origin.x,
currentFrame.origin.y,
currentFrame.size.width,
currentFrame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height)];
You can also add the segment control to a search bar, not the navigation item. If you're like me, I needed a large title + search bar + navigation item, which would be impossible with the current top answer.
#Abhishek 's answer is close. You would do something like the following:
// Create the search controller
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.scopeButtonTitles = ["Option 1", "Option 2"]
// Make sure the scope bar is always showing, even when not actively searching
searchController.searchBar.showsScopeBar = true
// Make sure the search bar is showing, even when scrolling
navigationItem.hidesSearchBarWhenScrolling = false
// Add the search controller to the nav item
navigationItem.searchController = searchController
definesPresentationContext = true
#Gurjit Singh, the line with .showsScopeBar = true is the solution to your problem.
you can custom navigationItem's titleView, like this:
self.navigationItem.titleView = segmentview
you should not add subview to the navigationbar, for after push or pop UIViewcontroller ,the subview is still exsit on the navigationbar.

Button on pop Up View doesn't work

I was trying to create a button on my custom pop up view. But created button doesn't recognise its action.
func createPopUp(){
view.userInteractionEnabled = false
scrollerView.userInteractionEnabled = false
var screenSize:CGRect = UIScreen.mainScreen().bounds
var screenHeight = screenSize.height
var screenWidth = screenSize.width
var popUpView : UIImageView
popUpView = UIImageView(frame:CGRectMake(0, 0, 530, 150));
popUpView.center = CGPointMake(screenWidth/2,screenHeight/2)
popUpView.backgroundColor = UIColor.whiteColor()
popUpView.tag = 81
self.view.addSubview(popUpView)
var vazgecButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
vazgecButton.frame = CGRectMake(0, 0, 265, 60)
vazgecButton.center = CGPointMake(250, 550)
vazgecButton.setTitle("VazgeƧ", forState: .Normal)
vazgecButton.tag = 79
vazgecButton.addTarget(self, action: "closePopUpView:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(vazgecButton)
}
First i disabled user interacton for view and my scrollerView. Then i created a image view for pop up view, to keep my button. Neither adding on to popUpView nor view work.
and my closePopUpView is:
func closePopUpView(sender: UIButton!){
println("pressed!!")
}
I searched for internet but i couldn't find a solution.

Removing the border in search bar at the navigation title

my nav bar look like the image attached below. How do I remove the black border?
And my code to generate that is as follow
var searchBar : UISearchBar = UISearchBar(frame: CGRectMake(-5, 0, 320, 44))
searchBar.autoresizingMask = UIViewAutoresizing.FlexibleWidth
searchBar.layer.borderWidth = 1
searchBar.searchBarStyle = UISearchBarStyle.Minimal
var searchBarView : UIView = UIView(frame: CGRectMake(0, 0, 310, 44))
searchBarView.autoresizingMask = nil
searchBarView.layer.borderWidth = 0;
searchBarView.layer.borderColor = UIColor.clearColor().CGColor
searchBar.delegate = self;
searchBarView.addSubview(searchBar)
perhaps also do what you did on the view,
searchBar.layer.borderColor = UIColor.clearColor().CGColor

Resources