How could i hide UIBarButtonItem and space that it takes?
For hiding i use this code:
searchButton.isEnabled = false
searchButton.tintColor = UIColor.clear
But it still uses the space and if i have a third element it looks like a gap between them.
Changing width does not solve my problem.
searchButton.width = 0.0
navigationItem.rightBarButtonItems Is an optional array of buttons on the right in the navigation bar. You go through it and remove a button from it.
The best solution that i found was searchButton.customView = UIView()
Related
I am trying to programmatically remove the keyboard shortcut bar that appears at the bottom of an iPad when an external keyboard is connected.
There are plenty of posts and answers with "solutions" to this, but none of them work with the latest iOS. The closest solution was such:
UITextInputAssistantItem* item = [self inputAssistantItem];
item.leadingBarButtonGroups = #[];
item.trailingBarButtonGroups = #[];
All this currently does is remove the buttons on the left side of the bar. And this does nothing also:
textField.autocorrectionType = UITextAutocorrectionTypeNo;
How can I "programmatically" remove this bar??
Sorry for using swift code.
You can try my idea:
change autocorrectionType of UITextField from .yes to no.
Get inputAssistantItem and change leadingBarButtonGroups and trailingBarButtonGroups to empty.
Source code example:
tfSearchNameHiragana.autocorrectionType = .no
let shortcut: UITextInputAssistantItem? = tfSearchNameHiragana.inputAssistantItem
shortcut?.leadingBarButtonGroups = []
shortcut?.trailingBarButtonGroups = []
Following the existing answers didn't do the job for me (iPadOS 14.7.1). Instead of hiding the toolbar, I've got an empty (therefore useless) grey bar sitting on top of my onscreen keyboard, hiding valuable screen real estate, especially on an landscape home-buttoned iPad model.
I found out, that additionally to setting the two empty arrays you need to set the inputAccessoryView to nil. Et voilà! The bar above the keyboard completely disappears.
textField.inputAssistantItem.leadingBarButtonGroups = []
textField.inputAssistantItem.trailingBarButtonGroups = []
textField.inputAccessoryView = nil
You may think that setting the view to nil alone does the job, but as it turned out, this works only in combination with setting the empty arrays.
From InterfaceBuilder, change Correction to No:
Or, from source:
item.autocorrectionType = .no
I have a navigation bar which includes a UISearchController, and I cannot find a way to get rid of the 1px bottom border below the navigation bar:
I am already using the tricks for removing the navigation bar bottom border as suggested in this answer and many others:
navigationBar.isTranslucent = false
navigationBar.setBackgroundImage(aTransparentImage, for: .default)
navigationBar.shadowImage = nil
If I don't set the searchController on the navigationItem of my view controller it's fine, there is no bottom border, but as soon as I set the searchController it appears.
Even the dirty hacks that look for a 1px UIImageView in the nav bar view hierarchy don't work, as it seems this view is in a separate tree of the hierarchy. It's the UIImageView highlighted in blue below:
I'm out of ideas 😕
Ok, a colleague of mine provided a solution. In viewWillAppear in the view controller which is showing the search bar do:
if let imageView = navigationItem.searchController?
.searchBar.superview?
.subviews.first?
.subviews.last as? UIImageView,
imageView.frame.height * UIScreen.main.scale == 1.0 {
imageView.isHidden = true
}
This is obviously highly dependent on the exact view hierarchy that UIKit is using for the search bar, and could stop working with any future release of iOS (it works on iOS 12). You could make it more resilient by searching the superview subviews for a 1px height UIImageView, but still, it's quite a hack.
But so far, it's the only solution I have found that works.
Try to add
self.extendedLayoutIncludesOpaqueBars = true
or
self.automaticallyAdjustsScrollViewInsets = false;
self.extendedLayoutIncludesOpaqueBars = true
in ViewDidLoad Method. It worked for me
I had setup two UIBarButtonItem on the left. Below is the screen shot of the wireframes of the screen, captured from debugging view hierarchy. Red box is the default back button and green box is the menu button.
From the screenshot, there is a gap between the back button image and menu button. The back button's view is occupying the extra space. I'm trying to figure out a way to get these two button close to each other.
I removed the "Back" text for the back button:
let backItem = UIBarButtonItem()
backItem.title = ""
self.backBarButtonItem = backItem
And added menu button:
let btn = UIBarButtonItem()
btn.customView = menu // it's a UIButton
self.leftItemsSupplementBackButton = true
self.leftBarButtonItem = menu
If it truly is the back buttons view, then just reduce the size of its views frame and you are good to go.
If it is an attribute of the main back bar button item they give you, then make a custom one that looks the same and give it the appropriate size.
If you are using a flexible space bar button item, then use a fixed space bar button item and set it appropriately.
You can also modify the value of a bar button view's location through the insetInPlace() that you use on the frame, but that will take some experimenting on the correct values to be used.
There are few options:
One is to insert an invisible bar button item and give it negative width like shown here https://stackoverflow.com/a/31804099/520810
Alternatively you can adjust image insets https://stackoverflow.com/a/22742322/520810
I have a UISearchBar and I would like to change the position of the initial magnify icon (the one that appears in the middle of the UISearchBar) as well as the color or icon.
So far I changed the tint and the icon image.
However, the new icon shows up only if I test the app on a simulator but on an actual device(both run iOS 9.3) it still shows the default icon.
UISearchBar.appearance().setImage(UIImage(named: "SearchbarIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
As for the magnify icon position, I want it in the left side, where it shows up if I activate the UISearchBar.
I found quite a lot of answers around here many of the provided solutions don't work on iOS 8.0+ or are in Objective-C and I am having some problems understanding them.
I tried to add a custom background containing the icon but it shows up at the bottom of the search bar while the icon is still there:
The background shows up OK if I change the background offset for Y to -44 (the height of the UISearchBar) but I can no longer type since it seems the entire text field is pushed up. I also tried to change the vertical offset for the SearchText to 44 to compensate but no luck. To be honest, I am not sure what Search Text and the Background offsets are supposed to do but I decided to give them a try.
Is there any way to accomplish this? Or maybe a different approach?
You can adjust the position of the search bar icon using
func positionAdjustmentForSearchBarIcon(_ icon: UISearchBarIcon) -> UIOffset
See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/occ/instm/UISearchBar/positionAdjustmentForSearchBarIcon:
You can use:
uiSearchBar.setPositionAdjustment(UIOffset, for: UISearchBar.Icon)
replace UIOffset with your desired offset value UIOffset(horizontal: CGFloatvertical: CGFloat) and UISearchBar.Icon with .search
#Michael - Thanks for all the help.
I managed to grab the UISearchBar's UITextField:
func customizeSearchBar()
{
for subview in srcRegimenSearchBar.subviews
{
for view in subview.subviews
{
if let searchField = view as? UITextField
{
let imageView = UIImageView()
let image = UIImage(named: "SearchBarIcon.png")
imageView.image = image;
imageView.frame = CGRectMake(0, 0, 0, 0)
/*imageView.frame = CGRectMake(100, 0, 20, 19)*/
searchField.leftView = imageView
searchField.leftViewMode = UITextFieldViewMode.Always
}
}
}
}
I wanted to change the position since I have access to the frame but it seems only the the top and height can be modified, at least the way I tried so I set the height and width to 0 (I couldn't find a way to make it nil or remove it completely) and I added an UIImageView with the new icon over in the left side of the UISearchbar and added a custom horizontal offset for the tint.
Not the best solution out there, I'm sure of it, but for now it works.
i've added a pageControl to the titleView using the interface builder. The problem is that it is not centered at all. I could actually just do a lot of white spaces after the leftBarButton text, but it is not ideal. What would be the solution?
Instead of adding your pagecontroller from Storyboard try doing this
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(x, y, xx, yy);
pageControl.numberOfPages = 2;
pageControl.currentPage = 0;
self.navigationItem.titleView = pageControl;
I came across this post while looking for an answer for the exact problem. I ended up solving it by adding UIPageControl inside an UIView and set the proper constraints to center itself inside the UIView. You can do this in storyboard easily and add this UIView to titleView. Hope this helps.
The title view is centered automatically (if it's possible), so it's probably being pushed to the left by the right bar button item. You should add a background color to your title view for debugging purposes, so you can see if this is true. If that's the problem, you need to make your titleView smaller so it doesn't run into either button.
The other possibility (which you could see if you had a background color) is that the page control is not centered in the titleView. If that's the problem, you need to add a centerX constraint to the page control.