Im using SVG images by converting them into PDF format. But sometimes, those barbutton items are becoming very big. Maximum of times am facing this issue is while using UIAlertController (Ex: Showing an alert, showing action sheet)
Here is the code i use to assign SVG as UIBarButtons
let search:UIButton = Utilities.getButton(forTitle: "", buttonType: .custom, image: IMAGE_SEARCH_SVG);
search.addTarget(self, action: #selector(handleSearchAction), for: .touchUpInside)
search.bounds = CGRect(x: 0, y: 0, width: H20, height: H20)
let searchBarBtnItem = UIBarButtonItem(customView: search)
self.navigationItem.rightBarButtonItem = searchBarBtnItem
u can see search icon on the top
on showing UIAlertAction,the search icon became big
Can someone help me in fixing this.
Related
I want to implement when I start editing inside search bar, then list would appear below the searchbar and I would select multiple values from list and when I want to select any text It would be add inside my search bar or textfield like button like this image below
this is example from stack overflow, so please guide me how to implement this I have no idea about this implementation, you can refer also third party library also and if you can help me with code. Thanks
may it will works
i suggested to use textfield as search engine
write code inside add button
let button = UIButton(type: .custom)
button.setTitle("name", for: UIControlState.normal)
button.frame = CGRect((x: CGFloat(txt.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25)))
button.addTarget(self,action: #selector(action), for: .touchUpInside)
textField.rightView = button
textField.rightViewMode = .always
use array to save the values
I am trying to recreate the search field as seen in the Yahoo Finance app. I followed an online tutorial for customizing the UISearchBar and UISearchController, however I still have some problems. If any of you could open up my project and see where Im going wrong / where I need to add these lines with even one of these that would be really great.
My attempted solution project can be found here: https://github.com/jordanw421/yahoofinance
1) How to get the search bar active (with text to the left, and typing indicator blinking) as soon as view presents itself? In the link below, you can see what I am talking about, when the button is pressed the search view is loaded and the search bar is instantly active.
https://youtu.be/tRtXm-m1hX0
I tried using:
customSearchController.definesPresentationContext = true
customSearchController.isActive = true
customSearchController.searchBar.becomeFirstResponder()
but that didn't work. Should I be setting these in the initial view controller with prepareForSegue?
2) How to set the keyboard appearance (to dark), and to add a keyboard toolbar with a button?
I was able to get this working for a non-custom search bar, but for some reason these don't work now:
customSearchController.searchBar.keyboardAppearance = .dark
and
func addKeyboardButton() {
let keyboardToolbar = UIToolbar()
keyboardToolbar.sizeToFit()
keyboardToolbar.isTranslucent = false
keyboardToolbar.barTintColor = UIColor.blue
let addButton = UIButton(type: .custom)
addButton.frame = CGRect(x: keyboardToolbar.frame.size.width / 2, y: keyboardToolbar.frame.size.height / 2, width: 50, height: 30)
addButton.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
let item = UIBarButtonItem(customView: addButton)
keyboardToolbar.items = [item]
customSearchController.searchBar.inputAccessoryView = keyboardToolbar
}
and calling,
addKeyboardButton()
in the search bar configure function.
3) How to prevent the search bar / table view header from scrolling, but still allow the tableView to scroll?
If you look at my attempted solution you can see that for some reason the tableview header scrolls with the table view. When I use a non-custom search bar the header remains static.
I know there are a lot of questions here, but I've been stuck on this for awhile and could really use some help. Thank you.
I have an iOS app written in Swift (2.1). I'm using a little workaround which I've found to make smaller margins between UIBarButtonItem's in NavigationBar. Here's my code:
let modeImage = UIImage(named: modeFilename)!.imageWithRenderingMode(.AlwaysTemplate)
modeButton = UIButton(frame: CGRectMake(0, 0, 22, 22))
modeButton.setBackgroundImage(modeImage, forState: .Normal)
modeButton.addTarget(self, action: Selector("modeClicked:"), forControlEvents:.TouchUpInside)
modeButton.transform = CGAffineTransformMakeTranslation(10, 0)
let modeButtonContainer = UIView(frame: modeButton.frame)
modeButtonContainer.addSubview(modeButton)
modeButtonItem = UIBarButtonItem(customView: modeButtonContainer)
Button works, but I have a square-like simple image and touch is only working on non-transparent pixels (or near them maybe). It's hard to hit it on simulator not to mention real device. It's a real pain. I've found solution to use Tap Gesture Recognizer on modeButtonContainer, but then button is not hovering so it's not a best solution.
Using setImage instead of setBackgroundImage doesn't change anything. Same goes for creating button with type UIButtonType.Custom.
Help!
Figured it out! My theory was wrong. It's not because of transparent pixels. Problem causes this line:
modeButton.transform = CGAffineTransformMakeTranslation(10, 0)
This caused button to be offsetted by 10 points instead being at center of its container. Button seemed looking good, but when I've set background to modeButtonContainer I realized button work only on containers area and doesn't work outside it.
So I needed to remove this line and instead use add this as first item in rightBarButtonItems:
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -10
I've been trying to find a solution to this problem for the past two days and still nothing I have tried works for me. Some solutions I have tried either stretch or completely mess up my custom image but nothing removes the right padding. Here is my code below as well as the result. Notice how the right bar button has a bigger space than the left system one.
var saveButton: UIButton = UIButton(frame: CGRectMake(0, 0, 32, 32))
saveButton.addTarget(self, action: "saveAction:", forControlEvents: .TouchUpInside)
let img = UIImage(named: "save")
saveButton.setBackgroundImage(img, forState: .Normal)
var saveBarButton: UIBarButtonItem = UIBarButtonItem(customView: saveButton)
self.navigationItem.setRightBarButtonItem(saveBarButton, animated: false)
Alright guys, I've found a solution that works pretty well for me. Instead of creating and setting the bar button item in code I added one in storyboard and set it's left margin in size inspector to -6 (or whatever value you like) and it's right margin to 6 (again whatever value you prefer note that the two values must be the same values but one is positive and the other is negative). Then I set it's image to the image I desired in the attributes inspector. If for some reason you want to change the image in code you can do so like this:
barButtonOutlet.image = UIIMage(named: "yourImage") as UIImage?
Hope this helps some of you out.
This is my solution. Try..
rightBtn.imageInsets = UIEdgeInsets(top: 0, left: -13.0, bottom: 0, right: 13.0)
self.navigationItem.rightBarButtonItem = rightBtn
I'm writing an app that gives a user tokens to spend and I want to display the user's current number of tokens in a UINavigationBar. What I would like to have is a label with the number of tokens, and an image of a coin in the top right corner of my navigation bar.
I've been searching for ways to customize the UINavigationBar, and have found plenty of posts related to adding an image to cover the entire bar, and changing the title. However, I can't find a simple way to do what I want.
I think I need to subclass UINavigationBar and add the text/image myself, but being new to iOS development and Swift, I was hoping that someone could point me in the right direction.
You can create a custom view contains subviews UILabel and UIImageView to show the token number and token image. Add it to right bar button item of the navigation controller.
It will look like:
Below code will create the custom view. Here you can observe that it is a local variable. However, you can manage global variable for custom view or create a whole new class and manage it independently for real-time updates to show token number.
// Custom to hold token number and image
let tokenView = UIView(frame: CGRect(x:0, y:0, width:100, height:44))
tokenView.backgroundColor = UIColor.yellow
// Label to show token number
let tokenLabel = UILabel(frame: CGRect(x:0, y:0, width:60, height:44))
tokenLabel.text = "1234"
tokenLabel.textAlignment = NSTextAlignment.right
let imageHeight = CGFloat(30)
let marginY = CGFloat((tokenView.frame.size.height / 2) - (imageHeight / 2))
// ImageView to display token image
let tokenImage = UIImageView(image: UIImage(named: "coin"))
tokenImage.frame = CGRect(x:70, y:marginY, width:30, height:30)
tokenView.addSubview(tokenLabel)
tokenView.addSubview(tokenImage)
// Add custom view as a right bar button item
let barButtonItem = UIBarButtonItem(customView: tokenView)
self.navigationItem.rightBarButtonItem = barButtonItem
To display the current number of tokens you can just set the title. In your view controller:
navigationItem.title = '\(numberOfCoins) coins"
If you want to do anything more fancy you can set your own title view instead of the standard UILabel:
navigationItem.titleView = UIView(...) // your custom view
To show the coin in the top right corner you'd set the rightBarButtonItem:
navigationItem.rightBarButtonItem = UIBarButtonItem(
image: UIImage(named:"Coin"),
style: .Plain,
target:self,
action:"tappedCoinButton:")