remove space below the UITabBarItem? - ios

how do I reduce the distance between the two items?
my code:
-(void)setupRightMenuButton{
filtro = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"icon-filtro-bar"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonFilter)];
busca = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"icon-busca"] style:UIBarButtonItemStylePlain target:self action:#selector(moveToSearchView)];
filtro.tintColor = [UIColor whiteColor];
busca.tintColor = [UIColor whiteColor];
filtro.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
busca.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:busca, filtro, nil] animated:YES];
}
My viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[self setupLeftMenuButton];
[self.navigationController.navigationBar setBarTintColor: [UIColor colorWithRed:0.49 green:0.075 blue:0.082 alpha:1]]; /*#7d1315*/
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
[self setupRightMenuButton];
self.title = #"Músicas";
}
I tried using the filtro.imageInsets = UIEdgeInsetsMake (6, 0, -6, 0); but it did not work

Try to add buttons this way:
UIButton *but1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)];
UIView *but1View = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[but1View addSubview:but1];
UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc] initWithCustomView:but1View];
UIButton *but2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)];
UIView *but2View = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[but2View addSubview:but2];
UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc] initWithCustomView:but2View];
self.navigationItem.rightBarButtonItems=#[ rightButton,rightButton2];
you need to mention the postions of x and y for the both UIViews as where ou want your buttons on the nav bar.

This code works for me. Try to create a button, then set the image to it and set the buttons frame.
let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
let image = UIImage(named: "your_image")!
button.setImage(image , forState: UIControlState.Normal)
// optional -------------
button.addTarget(self, action: "your_function", forControlEvents: UIControlEvents.TouchUpInside)
// ------------
button.frame = CGRectMake(0, 0, image.width, image.height)
let barButton = UIBarButtonItem(customView: button)
Create as many barButtons as you want and then add them to navigationItem:
self.navigationItem.setRightBarButtonItems([barBtn1, barBtn2], animated: false)

Related

IOS 11 Navigation Bar Button Item

We were using the below code to show leftbarbutton item in our navigation bar in IOS 10 SDK and XCode 8, however when we updated to xcode9 and ios 11 sdk. The left bar button item doesnt show. I searched through the web , but couldnt find any solution to that.
Does anyone know any answer?
UIBarButtonItem *cancelButton = nil;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"11")) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
//button.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
//button.contentEdgeInsets = UIEdgeInsetsMake(13, 8, 13, 8);
NSLayoutConstraint *widthConst = [button.widthAnchor constraintEqualToConstant:32.0];
NSLayoutConstraint *heightConst = [button.heightAnchor constraintEqualToConstant:32.0];
[button setImage:[UIImage imageNamed:#"cross"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
cancelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
widthConst.active = YES;
heightConst.active = YES;
} else {
cancelButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"cross"] style:UIBarButtonItemStylePlain target:loginRouter action:#selector(hide)];
}
cancelButton.tintColor = [UIColor whiteColor];
loginViewController.navigationItem.leftBarButtonItem = cancelButton;
loginRouter.loginViewController = loginViewController;
CardNavigationController *navigationController = [CardNavigationController darkBlueNavigationControllerWithClient:client];
navigationController.viewControllers = #[ loginViewController ];
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[onViewController presentViewController:navigationController animated:YES completion:nil];
And you can find the result in ios 11 below
Check the following example
UIImage *imgCart = [self imageWithImage:[UIImage imageNamed:#"ic_cart"] scaledToSize:CGSizeMake(35, 35)] ;
imgCart = [_utils changeColorOf:imgCart to:[UIColor whiteColor]];
UIButton *btnCart = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnCart addTarget:self action:#selector(btnCartClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnCart setBackgroundImage:imgCart forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCart];
It does not work when you set 'cancelButton.tintColor = [UIColor whiteColor]' in ios11. you can use the method "setTitleTextAttributes".

Custom Keyboard InputAccessoryView not visible in iOS 11

I have implemented Custom input accessory view it was working fine till iOS 10.3.1. But it's not visible in iOS 11 beta.
Have anyone experience this issue?
The question you ask does not have much detail. But I had the same problem when using an inputAccessoryView and a custom inputView for the textfield.
And resolved this on iOS11 by setting the custom inputView's autoresizingMask to .flexibleHeight.
yourCustomInputView.autoresizingMask = .flexibleHeight
Hope this resolves the issue. If not maybe provide some more information?
Here is how I add the input accessory, incase this is of more help (as extension of textfield):
public extension UITextField {
public func addToolbarInputAccessoryView(barButtonItems: [UIBarButtonItem],
textColour: UIColor,
toolbarHeight: CGFloat = 44,
backgroundColour: UIColor = .white) {
let toolbar = UIToolbar()
toolbar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: toolbarHeight)
toolbar.items = barButtonItems
toolbar.isTranslucent = false
toolbar.barTintColor = backgroundColour
toolbar.tintColor = textColour
inputAccessoryView = toolbar
}
}
And then on the inputView (not the inputAccessoryView), I was using a date picker for example - just make sure that the date picker's autoresizing mask is set to flexible height.
PSA: If you use a UIToolbar as your custom view, it's currently broken in iOS 11 GM.
Instead of loosing your hair on how to fix it, just change it to UIView.
You'll loose the blur effect but it will work.
Beta 3 has just come out and some people said it solved the problem, but for me it didn't.
However I tried setting the accessory view to something stupid (100pxls high) and spotted that the Undo/Redo/Paste bar on the iPads was incorrectly sitting over the top of my accessory bar.
So I added the following code to get rid of Apples bar (it was pointless for my custom picker anyway) and the problem went away
Hope this helps somebody
- (void)textFieldDidBeginEditing:(UITextField*)textField
{
UITextInputAssistantItem* item = [textField inputAssistantItem];
item.leadingBarButtonGroups = #[];
item.trailingBarButtonGroups = #[];
}
To avoid the inputAccessoryView issue in iOS 11 for UITextField and UITextView, just use the following code:
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
self.monthPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
self.monthPickerView.backgroundColor = [UIColor whiteColor];
self.monthPickerView.delegate = self;
self.monthPickerView.dataSource = self;
[inputView addSubview:self.monthPickerView];
cell.monthTextField.inputView = inputView ;
self.monthTextField.inputAccessoryView = [self doneButtonAccessoryView];
// doneButtonAccessoryView Method
-(UIToolbar*)doneButtonAccessoryView
{
UIToolbar *kbToolbar = [[UIToolbar alloc] init];
[kbToolbar sizeToFit];
[kbToolbar setBarTintColor:[UIColor whiteColor]];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleDone target:self
action:#selector(cancelClicked)];
NSDictionary *attrDict;
attrDict = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica-Bold" size:16.0], NSFontAttributeName, nil];
[doneButton setTitleTextAttributes:attrDict forState:UIControlStateNormal];
UIBarButtonItem *flexWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
[kbToolbar setItems:[NSArray arrayWithObjects:cancelButton,flexWidth, doneButton, nil]];
return kbToolbar;
}
UIToolBar is broken in iOS 11. But you can get the same thing done using UIView as inputAccessoryView. Sample code snippet here:
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
UIView* toolBar = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f, width, 44.0f)];
toolBar.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.0f];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0 , 0.0f, width, 44.0f)];
[titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:13]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor redColor]];
[titleLabel setText:#"Title"];
[titleLabel setTextAlignment:NSTextAlignmentLeft];
[toolBar addSubview:titleLabel];
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
doneBtn.tintColor = [UIColor colorWithRed:(float)179/255 green:(float)27/255 blue:(float)163/255 alpha:1];
[doneBtn.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:16]];
[doneBtn addTarget:self action:#selector(btnTxtDoneAction) forControlEvents:UIControlEventTouchUpInside];
[doneBtn setFrame:CGRectMake(width-70, 6, 50, 32)];
[toolBar addSubview:doneBtn];
[toolBar sizeToFit];
txtMessageView.inputAccessoryView = toolBar;
Hope this help..:)
I've had the same issue and I've found out that removing all of the bottom, top, leading, training, left, right constraints for the
view that is assigned accessoryView solved it.
Swift 4 solution
let toolBarRect = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44)
let toolBar = UIView(frame: toolBarRect)
toolBar.backgroundColor = .lightGray
let nextButton = UIButton()
nextButton.setTitleColor(.black, for: .normal)
nextButton.setTitle("Next", for: .normal)
nextButton.addTarget(self, action: #selector(self.onNextButtonTouch), for: .touchUpInside)
nextButton.translatesAutoresizingMaskIntoConstraints = false
toolBar.addSubview(nextButton)
NSLayoutConstraint.activate(
[
nextButton.heightAnchor.constraint(equalToConstant: Constants.keyboardToolBarHeight),
nextButton.trailingAnchor.constraint(equalTo: toolBar.trailingAnchor, constant: -16),
nextButton.centerYAnchor.constraint(equalTo: toolBar.centerYAnchor, constant: 0)
]
)
self.yourTextField.inputAccessoryView = toolBar
Just in case someone might still need the solution, here's what I did (IOS 12.1);
private func initSearchBox() {
// Add Done button on keyboard
txtSearch.delegate = self
let tbrDone = UIToolbar()
let btnDone = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(btnDone_tapped))
tbrDone.items = [btnDone]
tbrDone.sizeToFit()
self.txtSearch.inputAccessoryView = tbrDone
}
#objc func btnDone_tapped() {
view.endEditing(true)
}

How to have two different colors for two UIBarButtonItem in navigation bar at the same time?

Are there any standard methods for having two different colors for two UIBarButtonItems on a navigationBar at the same time?
This could be achieve using custom UIButton, e.g.:
UIButton *confirmBtn = [[UIButton alloc] init];
confirmBtn.titleLabel.font =[UIFont boldSystemFontOfSize:16.0f];
[confirmBtn setTitle:#"Confirm" forState:UIControlStateNormal];
confirmBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -40);
[confirmBtn setTitleColor:[ColorSetter colorWithHexString:#"ffd420"] forState:UIControlStateNormal];
confirmBtn.frame = CGRectMake(0, 0, 80, 50);
[confirmBtn addTarget:self action:#selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *confirmBtnBarItem = [[UIBarButtonItem alloc] initWithCustomView:confirmBtn];
self.navigationItem.rightBarButtonItem = confirmBtnBarItem;

Why my UIButton doesn't display in my subview? iPad App

Could somebody help me to understand why my UIButton doesn't display in my custom UIView?
In the code below I posted initWithFrame method:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor =[UIColor whiteColor];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, 50)];
toolbar.backgroundColor = [UIColor grayColor];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissNewInvoiceView:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveNewInvoice:)];
[saveBarButton setEnabled:NO];
NSArray *arrayOfItems = [[NSArray alloc]initWithObjects:cancelBarButton,flexibleSpace, saveBarButton, nil];
[toolbar setItems:arrayOfItems];
UILabel *headerViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(335, 15, 200, 20)];
headerViewLabel.text = #"New Invoice";
[toolbar addSubview:headerViewLabel];
[self addSubview:toolbar];
UIButton *selectCustomerButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 60, 60)];
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"];
[self addSubview:selectCustomerButton];
[self bringSubviewToFront:selectCustomerButton];
UILabel *invoiceNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 55, 150, 25)];
[invoiceNumberLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
invoiceNumberLabel.text = #"Invoice number:";
[self addSubview:invoiceNumberLabel];
UITextField *invoiceNumberField = [[UITextField alloc]initWithFrame:CGRectMake(140, 53, 150, 30)];
[invoiceNumberField setFont:[UIFont fontWithName:#"Verdana" size:25]];
invoiceNumberField.placeholder = #"25/13";
[self addSubview:invoiceNumberField];
UILabel *dateOfCreationLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 55, 150, 25)];
[dateOfCreationLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
dateOfCreationLabel.text = #"Date of creation:";
[self addSubview:dateOfCreationLabel];
}
return self;}
Why UILabels are displaying properly by implement addSubview method while UIButtons are not?
Regards
Don't do this:
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"]
Use
[selectCustomerButton setImage:[UIImage imageNamed:#"Farmer.png"] forState:UIControlStateNormal];
The button sets its own image views image depending on its state, you don't access and set the image view's image yourself.
The button will be just invisible in case the image is not loaded/shown properly. As jrturton already said, you should use another method to set the image.
To figure out if the button is 'there', you can give the button a backgroundcolor like this:
selectCustomerButton.backgroundColor = [UIColor redColor];
If you see a red box, you know the image is not properly loaded or maybe not even found.

Setting a title to a UIToolBar in IOS 7

I have a UIToolBar button declared below:
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 64.0);
I also have a UIBarButtonItem that gets added in on the left as a back button. But how do I just add a centered label title to the UIToolbar?
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 20)];
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
lblTitle.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:lblTitle];
UIBarButtonItem *typeField = [[UIBarButtonItem alloc] initWithCustomView:lblTitle];
toolBar.items = [NSArray arrayWithArray:[NSArray arrayWithObjects:backButton,spaceBar,lblTitle, nil]];
This will solve your problem i think.
Create a UILabel and add it as an item of the toolbar:
UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:myLabel];
If you want to center it, add flexible spaces on the sides:
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
None of the other answers were as copy and pasteable as I wanted, so...
UILabel *toolbarLabel = [[UILabel alloc] initWithFrame:CGRectZero];
toolbarLabel.text = #"Text in yo toolbar";
[toolbarLabel sizeToFit];
toolbarLabel.backgroundColor = [UIColor clearColor];
toolbarLabel.textColor = [UIColor grayColor];
toolbarLabel.textAlignment = NSTextAlignmentCenter;
UIBarButtonItem *labelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self setToolbarItems:#[flexible, labelItem, flexible] animated:false];
This assumes you have the toolbar showing through your navigation controller (to show it from any View Controller that's shown by a navigation controller, [self.navigationController setToolbarHidden:NO animated:YES];)
It looks something like this (iOS 9):
UIToolbar doesn't have a title property. As others suggested you need to add a UILabel as a title.
Or you can use UINavigationBar instead and initialize using - (id)initWithTitle:(NSString *)title;
Here is a Swift 2.2 implementation of Aaron Ash's answer
let toolbarLabel = UILabel(frame: CGRectZero)
toolbarLabel.text = "Text in yo toolbar"
toolbarLabel.sizeToFit()
toolbarLabel.backgroundColor = UIColor.clearColor()
toolbarLabel.textColor = UIColor.grayColor()
toolbarLabel.textAlignment = .Center
let labelItem = UIBarButtonItem.init(customView: toolbarLabel)
let flexible = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target:nil, action:nil)
self.setToolbarItems([flexible, labelItem, flexible], animated: false)
Here is a Swift 3 implementation:
let topBarTitleLabel = UILabel.init(frame: (CGRect.init(origin: CGPoint.init(x: 0.0, y: 0.0), size: CGSize.init(width: 0.0, height: 0.0))))
topBarTitleLabel.text = "Text in yo toolbar"
topBarTitleLabel.sizeToFit()
topBarTitleLabel.backgroundColor = UIColor.clear
topBarTitleLabel.textColor = UIColor.black
topBarTitleLabel.textAlignment = NSTextAlignment.center
let topBarButtonItemTitleLabel = UIBarButtonItem.init(customView: topBarTitleLabel)
let flexibleBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
self.topToolBar.setItems([flexibleBarButtonItem, topBarButtonItemTitleLabel, flexibleBarButtonItem], animated: false)
self.topToolBar.setNeedsLayout()

Resources