I am trying to do a custom toolbar that shows image and label but changes according to which view it is in. I have scoured the internet and read and tried everything I could find on the subject.
I figured that putting a tab bar was not a solution since it can't change for every view (or can it?).
Last time I asked I was marked duplicate and quoted Set image and title for bad button item?. I tried that but I could not get that to work at all. Ideally, I wanted a storyboard solution.
I'll show what I have done and tried with so far. What am I doing wrong? Have I missed something? I can't get the title to show on the toolbar item.
Xcode main.storyboard current toolbar version:
Sketch version (what I want it to look like):
What I hope it will change to in subsequent view:
Code:
class ViewController: UIViewController {
#IBOutlet weak var toolbar: UIToolbar!
#IBOutlet weak var leftBarButton: UIBarButtonItem!
var middleBarButton: UIBarButtonItem!
var rightBarButton: UIBarButtonItem!
var rightBarButton2: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView()
let button = UIButton(type: .system)
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "IconHelp"), for: .normal)
button.setTitle("Help", for: .normal)
button.addTarget(self, action: #selector(about), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
// var items = self.toolbar.items
// items![0] = UIBarButtonItem(customView: view)
leftBarButton = UIBarButtonItem(customView: button)
Any help would be much appreciated as I've tried to solve this all week. :P
Related
I have 2 UIViews that I converted to UIColorWells.
#IBOutlet weak var colorWell1: UIColorWell!
#IBOutlet weak var ColorWell2: UIColorWell!
When I run the app, I see the color wells just fine (see image below). However, when I try to click on them, the actual color picker never opens. It seems like its just the image of the color well showing up.
Is there method I have to create to make them open, or is it built in? This is what I have pertaining to the colorWells below:
override func viewDidLoad() {
super.viewDidLoad()
colorWell1.addTarget(self, action: #selector(colorWellChanged(_:)), for: .valueChanged)
colorWell2.addTarget(self, action: #selector(colorWellChanged(_:)), for: .valueChanged)
}
#objc func colorWellChanged(_ sender: Any) {
colorWell1.backgroundColor = colorWell1.selectedColor
colorWell2.backgroundColor = team2ColorWell.selectedColor
}
This is what the colorWells look like: enter image description here
This is my folder structure:
enter image description here
I Would like to implement a Favorite button in my detailVC.
I Have a tableView with a list of restaurants in the area :
ResterauntTableViewController:
import UIKit
class ResterauntViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate{
#IBOutlet var tableViewController: UITableView!
// MARK : Data
var names = ["Shalvata",
"Markid",
"Litzman Bar",
"The Cat & The Dog",
"Light house",
"Ku"]
it looks like this :
Each cell has a DetailViewController and a favorite button i wish to create.
I have already set up a button that is changing the image if i click on the button and vice versa right here :
override func viewDidLoad() {
super.viewDidLoad()
//create a new button
let Favoritebutton: UIButton = UIButton(type: UIButtonType.Custom)
//set image for button
Favoritebutton.setImage(UIImage(named: "EmptyHeart.png"), forState: UIControlState.Normal)
Favoritebutton.setImage(UIImage(named: "FilledHeart.png"), forState: UIControlState.Selected)
//add function for button
Favoritebutton.addTarget(self, action: #selector(self.button(_:)), forControlEvents: .TouchUpInside)
//set frame
Favoritebutton.frame = CGRectMake(90, 90, 35, 35)
let barButton = UIBarButtonItem(customView: Favoritebutton)
//assign button to navigationbar
self.navigationItem.rightBarButtonItem = barButton
}
#IBAction func button(sender: UIButton) {
sender.selected = !sender.selected;
}
}
Once Favorite button is clicked i want to show the item wish was clicked and set as favorite to show in another tab ( i have a tab bar project )
and of course when i quit the app it will remember my favorites.
i managed to do the button myself but i don't know how to create the favorite function so i would like to get some help.I couldn't find any good tutorial showing any of it either.
Thanks for helping.
It is very broad question you are asking...
You need some kind of persistence to save the like button action. I recommend using SQLite or Core Data to store it, and then you can read it on the FavoriteTab
Here is a very good tutorial SQLite Tutorial: Getting Started
Here is a very good tutorial on User Defaults
I have 2 button like picture below, their text label are: "Input location...". Now i want to click on the swap button on the right, so the text label of these button will swap. It's look like you have 2 location and you want to swap it. Please any one can point me how to do it. Thank you very much.
On click just set their text to each other.
var tempString = secondButton.titleLabel.text
secondButton.setTitle(firstButton.titleLabel.text, forState: UIControlState.Normal)
firstButton.setTitle(tempString, forState: UIControlState.Normal)
#IBOutlet var firstButton: UIButton!
#IBOutlet var secondButton: UIButton!
#IBAction func didTouchUpInsideSwapButton() {
let firstButtonText = firstButton.titleLabel?.text
firstButton.setTitle(secondButton.titleLabel?.text, for: .normal)
secondButton.setTitle(firstButtonText, for: .normal)
}
I'm making a custom keyboard using swift on Xcode.
I have created some views(xib files) which are of keyboards.
and I wanna connect these views each other like when I press [123]button, the view changes to the numeric one so I can type numbers :)
Actually I finished globe button(which is next keyboard button which is auto-created when I added custom keyboard target) but can't understand the codes ;-;
What I want to do is to connect button(as an IBOutlet or IBAction) with view(xib)
Take two views in xib or storyboard and IBOutlet them. One view has the buttons for alphabets and other view has buttons for numbers.
You have to do code something like below :
#IBOutlet var alphabelView: UIView!
#IBOutlet var numericView: UIView!
#IBOutlet var toggleBtn: UIButton!
btn.setTitle("ABC", forState: .Normal)
btn.setTitle("123", forState: .Selected)
toggleBtn.addTarget(self, action: #selector(self.toggleButtonsClicked(_:)), forControlEvents: .TouchUpInside)
Numeric view will be hidden initially. When user press on 123 do code like below lines of code:
func toggleButtonsClicked(button: UIButton) {
button.selected = !butto.selected
alphabelView.hidden = true
numericView.hidden = false
}
Recently, I have started to study Swift with story board, Xcode, etc.
I am struggling to understand the principals of ViewController code, as it seems to miss a lot of essentials - presumably to try and make things more simple - but it just isn't for programmers who have come from else where.
import UIKit
class ViewController: UIViewController {
//Properties
#IBOutlet weak var textFieldName: UITextField!
#IBOutlet weak var submit: UIButton!
#IBOutlet weak var submit1: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//Actions
#IBAction func onSubmit(sender: UIButton) {
switch(sender){
case submit:
print("only 1");
case submit1:
print("222");
default:
print("nothing");
}
}
}
After following a basic tutorial, I have tried playing around on my own to test out my understanding. The IBOutlets, which define elements of the storyboard requires me to actually drag and drop it into the code in order for it to correctly correspond to the element. When I just type
#IBOutlet weak var submit1: UIButton!
Without having dragged it from storyboard - it does not work / correspond. So essentially there is more backend that I do not have control over?
Also, Actions. How come it prints without the method being called?! I must be missing something obvious - something to do with the attributes?
I want to be able to code things myself, and identify elements/buttons/etc myself without dragging and dropping, and not seeing the associations - submit1 and submit, at the moment, are set to the exact same thing: UIButton!
I am aware and accept my confusion and interpretation is clearly flawed and wrong. But could someone shed some light on the lower level activities of ViewController?
Thanks.
If you don't want to drag and drop you can write, i don't see where is the problem
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .greenColor()
button.setTitle("Test Button", forState: .Normal)
button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
self.view.addSubview(button)