I created a segmented control using
private let updateSwitch: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Sign In", "Sign Out"])
sc.anchor(width: 128, height: 32)
sc.selectedSegmentIndex = 0
sc.tintColor = .mainBlue
sc.backgroundColor = .mainGray
sc.addTarget(self, action: #selector(handleSegmentedControlSwitch(_:)), for: .valueChanged)
return sc
}()
I then added updateSwitch to my navigation bar using
navigationItem.titleView = updateSwitch
The segmented control shows up completely fine but whenever I select either sign in or sign out, the selector code does not get executed
#objc func handleSegmentedControlSwitch(_ segmentedControl: UISegmentedControl) {
switch(segmentedControl.selectedSegmentIndex) {
case 0:
print("Sign In")
break
case 1:
print("Sign Out")
break
default:
break
}
}
Any idea on how I can fix this?
Attached is an image of the working segmented control
It's because of your UISegmentControl declaration.
You have 2 ways:
1. declare it as a lazy var:
because Self in lazy var is valid.
private lazy var updateSwitch: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Sign In", "Sign Out"])
sc.anchor(width: 128, height: 32)
sc.selectedSegmentIndex = 0
sc.tintColor = .mainBlue
sc.backgroundColor = .mainGray
sc.addTarget(self, action: #selector(handleSegmentedControlSwitch(_:)), for: .valueChanged)
return sc }()
2. assign action in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
let segmented = self.updateSwitch
segmented.addTarget(self, action: #selector(handleSegmentedControlSwitch(_:)), for: .valueChanged)
navigationItem.titleView = segmented
}
Related
I'm adding labels and buttons to a TextView dynamically. This is driven by looping through arrays containing label names, button names, and button function names. For brevity and simplicity, I've changed the code here to use hard-coded indexes rather than a loop, and I did not include the label- and button-creation functions. The labels are created just fine. The first button is also created just fine, since I specify Button1Clicked as the #selector argument. The second button is created correctly only if I specify Button2Clicked as the #selector argument. If I attempt to reference an array in the button #selector argument, I receive the compiler error "Argument of '#selector' does not refer to an '#objc' method, property, or initializer". Assuming it is possible to use an array containing the #objc function name as an argument to the #selector, can someone please provide the appropriate syntax for me? I'm using Swift 5 and Xcode 11.2. Thank you in advance for your assistance.
#objc func Button1Clicked(sender: UIButton) {
print("You pressed button 1")
}
#objc func Button2Clicked(sender: UIButton) {
print("You pressed button 2")
}
.
.
.
var labelNames = [UILabel]()
let Label1:UILabel = UILabel()
let Label2:UILabel = UILabel()
labelNames = [Label1, Label2]
var buttonNames = [UIButton]()
let Button1:UIButton = UIButton()
let Button2:UIButton = UIButton()
buttonNames = [Button1, Button2]
let buttonSelectorNames = ["Button1Clicked", "Button2Clicked"]
configureLabel(labelNameIn: labelNames[0],
textIn: "aaaaa",
textColorIn: appColorWhite,
backgroundColorIn: appColorBlue,
yPositionIn: 0)
statusTextview.addSubview(labelNames[0])
configureLabel(labelNameIn: labelNames[1],
textIn: "-- bbbbb",
textColorIn: appColorWhite,
backgroundColorIn: appColorBlue,
yPositionIn: 25)
statusTextview.addSubview(labelNames[1])
configureButton(buttonNameIn: buttonNames[0],
xPositionIn: 0,
yPositionIn: 50,
textIn: "B1",
textColorIn: appColorBlack,
backgroundColorIn: appColorBrightGreen,
textViewIn: statusTextview)
buttonNames[0].addTarget(self,
action: #selector(Button1Clicked),
for: .touchUpInside)
statusTextview.addSubview(buttonNames[0])
configureButton(buttonNameIn: buttonNames[1],
xPositionIn: 100,
yPositionIn: 50,
textIn: "B2",
textColorIn: appColorBlack,
backgroundColorIn: appColorBrightGreen,
textViewIn: statusTextview)
buttonNames[1].addTarget(self,
action: #selector(buttonSelectorNames[1]),
for: .touchUpInside)
statusTextview.addSubview(buttonNames[1])
you don’t need to define button click event for each button.you can define common function and assign it to all buttons in the loop.then you can specify through button tag
in your loop assign the current index as button tag
buttonNames[currentIndex].tag = currentIndex
buttonNames[currentIndex].addTarget(self,
action: #selector(didButtonClicked(_ :)),
for: .touchUpInside)
button click function
#objc func didButtonClicked(_ sender : UIButton){
switch sender.tag{
case 0: //clicked button with tag 0
case 1: //clicked button with tag 1
default: break
}
}
In the past, I have a deal with the same type of requirement. I was doing it by storing the selector in Array.
Please refer to the code snippets. I am sure it will help you.
Code:
class ViewController: UIViewController {
let selector = [#selector(Button1Clicked),#selector(Button2Clicked)]
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton(frame: self.view.bounds)
self.view.addSubview(btn)
btn.addTarget(self, action: selector[0], for: .touchUpInside)
}
#objc func Button1Clicked(){
print("Button1Clicked")
}
#objc func Button2Clicked(){
}
}
Output:
I'm trying to attach a search bar to the top of my tableView and change its attributes (eg. colour, placeholder). However, I can't figure out how. I've tried embedding the tableView in another view but that didn't help. Any ideas?
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.tintColor = UIColor.red
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
navigationItem.searchController = search
tableView.tableHeaderView = search.searchBar
}
This function is called in the viewDidLoad() and the tableView is added but not with the right colour or placeholder and jumps to the top of the screen when selected.
Any help would be appreciated.
This is the updated code for setupSearch (everything is working fine except the bar jumps to the top when selected):
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = search.searchBar
}
I declare the search bar at the start using:
let search = UISearchController(searchResultsController: nil)
Any ideas on how to stop the bar jumping to the top?
Just add search bar as tableview's headerview not with navigation's item searchcontroller(not add search bar with both(tableview and navigation) as in your code). You can try with updated code below:
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
tableView.tableHeaderView = search.searchBar
self.definesPresentationContext = true
}
override func viewDidLoad() {
super.viewDidLoad()
setupSearch()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) // be notified when the keyboard changes your table View frame
}
#objc func keyboardWillShow(notification: NSNotification) {
tableView.frame.origin.y = 0 // reset the table view to its original coordinates
}
func setupSearch(){
// for iOS 12 and lower, you can change the placeholder like this :
let textFieldSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldSearchBar?.textColor = .red
let searchBarLabel = textFieldSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldSearchBarLabel?.textColor = .red
}
I am learning some auto layout programatically and I want to add a button on the bottom part of the screen, just above the safe area.
I know the code is working, because I tested it in another project, I think it is a conflict because I get to this viewController from another one.
The code for my button
private let previousButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Prev", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
The code for setting up my constraints
fileprivate func setupBottomControls() {
view.addSubview(previousButton)
previousButton.backgroundColor = .red
view.addSubview(previousButton)
NSLayoutConstraint.activate([
previousButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
previousButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
previousButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
previousButton.heightAnchor.constraint(equalToConstant: 50)
])
}
Like I said this code is working in another project but I think it is in conflict because of how I called this viewController.
This is a code from the first view controller that make the new viewController(GettingStartedController) to be shown, here it will be the button mentioned above.
func switchVC(){
//enter GettingStarted controller
let controller = GettingStartedController()
view.addSubview(controller.view)
present(UINavigationController(rootViewController: controller), animated: true,completion: nil) //used when no animation is present
}
I think the problem is here any ideas on how to change the call to the GettingStartedController so that will see the Safe Area the right way?
Check my code solution. I added your function to the button when it is tapped and it now presents the GettingStartedController():
private let previousButton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("Prev", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(switchVC), for: .touchUpInside)
return button
}()
fileprivate func setupBottomControls() {
view.addSubview(previousButton)
previousButton.backgroundColor = .red
view.addSubview(previousButton)
NSLayoutConstraint.activate([
previousButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
previousButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
previousButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
previousButton.heightAnchor.constraint(equalToConstant: 50)
])
}
#objc func switchVC(){
//enter GettingStarted controller
let controller = GettingStartedController()
present(UINavigationController(rootViewController: controller), animated: true,completion: nil) //used when no animation is present
}
i have been coding for a while i am stuck with tableview single selection/deselection.
I have a tableview with custom cell which has a button. if i click on one button another button in bottom gets selected as well.For example when i click on button with index 2 another button gets click in the button.
it should be like when i click on one button other buttons should be deselected.
Thanks!
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected == true {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.isSelected = false
}
else {
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
cell.EventReviewQuickeReviewBtn.isSelected = true
}
}
Cells get reused. You need to ensure the exact same set of properties are set/reset in both conditions.
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.white // or whatever
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.red, for: .normal) // or whatever
} else {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.black // or whatever
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.green // or whatever
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 3 // or whatever
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
}
cell.EventReviewQuickeReviewBtn.isSelected = !cell.EventReviewQuickeReviewBtn.isSelected
}
when you select any button then you should have mark isSelected property to false
assuming you have a list of models for your table view like
class Example{
...
var isSelected: Bool
...
}
(in ViewController)
let list: [Example] = [example1, example2, example3]
on tap of any button deselect all then selected desire button like
list.forEach { (example) in
example.isSelected = false
}
list[selectedButtonIndex].isSelected = true
and then reload your tableView.
There is no method similar to the addtextfield method in Uialertcontroller. I haven't found any examples of how I can customize MDCAlertControllers. Anybody have an idea?
I think this is not possible. The docs say:
MDCAlertController class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
This is now possible by using accessoryView.
Simply set it to be your custom view and set the alert message to be blank.
alertController.accessoryView = myCustomView
Code example (using SnapKit for layout)
let title = "My title"
let alertController = MDCAlertController(title: title, message: "")
let confirmAction = MDCAlertAction(title:"Confirm") { [weak self] _ in
//your action here
}
let cancelAction = MDCAlertAction(title:"Cancel")
let width = UIScreen.main.bounds.width * 0.91
let testView = UIView()
let button = UIButton()
button.setTitle("Test", for: .normal)
testView.addSubview(button)
button.snp.makeConstraints { (make) in
make.center.equalToSuperview()
}
testView.backgroundColor = .blue //just to show where the view is
testView.snp.makeConstraints { (make) in
make.width.equalTo(width)
make.height.equalTo(100)
}
alertController.accessoryView = testView
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
alertController.defaultTheming()
present(alertController, animated:true)
Result from above code