Switch Control to Toggle Ishidden

How to remove or hide the label of a Toggle in SwiftUI

SwiftUI 1.0

Hide the label/title with the labelsHidden Modifier

This is how it should be done.

Toggle("Turn alarm on", isOn: $isToggleOn)
.labelsHidden() // Hides the label/title

Note: Even though the label is hidden, you should still add one for accessibility purposes.

Example:

Toggle Example with no Label

Toggle hidden state of UILabel

To make the code shorter (and look better) I would do like this if its just a toggle:

for label in [label1, label2, label3, label4] {
label.hidden = !label.hidden
}

This will toggle label1-4.hidden

Press search button to show the searchBar from isHidden to isSelected

I don't know what you need exactly but if you need to switch isHidden or isSelected you can add var to keep the search bar state like:

var isSearchFieldHidden: Bool = false{
didSet{
self.searchField.isHidden = isSearchFieldHidden
}
}

@IBAction func searchButton(_ sender: Any) {
isSearchFieldHidden.toggle()
}

@IBOutlet weak var searchField: UISearchBar!

Create toggle button behaviour to hide or show div

Since you are really toggling between adding either the button-dark or the button-positive class to the buttons you can do it like this:

Updated (x2)

<ion-content> 
<div ng-controller="StatementsController"> <!-- move the ng-controller out here -->
<div class="row" style="margin-bottom: -1.35em">
<div class="col">
<button class="button button-clear"
ng-class="{'button-positive': active, 'button-dark': !active}"
ng-click="toggle('list')">List View</button>
</div>
<div class="col col-right">
<button class="button button-clear"
ng-class="{'button-positive': !active, 'button-dark': active}"
ng-click="toggle('card')">Card View</button>
</div>
</div>
<div>
<!-- List view -->
<div ng-show="active">
test 1
</div>
<!-- Card view -->
<div ng-hide="active">
test 2
</div>
</div>
</div>
</ion-content>

(Swift) How to hide some sections in tableView when toggle switch is on?

Approach:

In your Model, you can create a isHidden property that will keep a track of whether the section should be hidden or not, i.e.

class Model {
var sectionName: String
var isHidden = false

init(sectionName: String) {
self.sectionName = sectionName
}
}

Now, modify the UITableViewDataSource methods to,

class VC: UIViewController, UITableViewDataSource, UITableViewDelegate {
let arr = [Model(sectionName: "Section 1"), Model(sectionName: "Section 2"), Model(sectionName: "Section 3"), Model(sectionName: "Section 4"), Model(sectionName: "Section 5")]
lazy var dataSource = self.arr

func numberOfSections(in tableView: UITableView) -> Int {
return dataSource.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.cellSwitch.setOn(false, animated: false)
cell.labelCell.text = dataSource[indexPath.section].sectionName
cell.callback = {[weak self] in
guard let `self` = self else {
return
}
self.arr[indexPath.section].isHidden = !(self.arr[indexPath.section].isHidden)
self.dataSource = self.arr.filter({ !$0.isHidden })
tableView.reloadData()
}
return cell
}
}

And you can simply call the closure callback?() in toggleSwitch(_:) and the hiding/unhiding will be handled automatically.

class TableViewCell: UITableViewCell {
@IBOutlet weak var labelCell: UILabel!
@IBOutlet weak var cellSwitch: UISwitch!

var callback:(()->())?
@IBAction func toggleSwitch(_ sender: Any) {
callback?()
}
}

Toggle function with UIButton programmatically in swift

Try this

class ViewController: UIViewController { 
let switchLightBT = UIButton()
let switchDarkBT = UIButton()

override func viewDidLoad() {
super.viewDidLoad()
switchLightBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102))
switchLightBT.backgroundColor = .clear
switchLightBT.tag = 1
switchLightBT.setImage(UIImage(named:"light"), for: .normal)
switchLightBT.addTarget(self, action: #selector(darkTheme(sender:)), for: .touchUpInside)
self.view.addSubview(switchLightBT)

switchDarkBT.frame = (frame: CGRect(x: 1, y: 220, width: 102, height: 102))
switchDarkBT.backgroundColor = .clear
switchDarkBT.tag = 2
switchLightBT.setImage(UIImage(named:"dark"), for: .normal)
switchDarkBT.addTarget(self, action: #selector(lightTheme(sender:)), for: .touchUpInside)

self.view.addSubview(switchDarkBT)

}

func darkTheme(sender: UIButton!){
self.view.backgroundColor = .black
switchLightBT.isHidden = true
switchDarkBT.isHidden = false

}

func lightTheme(sender: UIButton!){
self.view.backgroundColor = .white
switchLightBT.isHidden = false
switchDarkBT.isHidden = true

}

Dynamically hiding view in SwiftUI

Rather than dynamically setting a variable and using it in my view, I found that I was able to hide or show the date picker this way:

struct ContentView : View {
@State var showDatePicker = true
@State var datePickerDate: Date = Date()

var body: some View {
VStack {
if self.showDatePicker {
DatePicker($datePickerDate)
} else {
DatePicker($datePickerDate).hidden()
}
}
}
}

Or, optionally, not including the date picker instead of hiding it:

struct ContentView : View {
@State var showDatePicker = true
@State var datePickerDate: Date = Date()

var body: some View {
VStack {
if self.showDatePicker {
DatePicker($datePickerDate)
}
}
}
}


Related Topics



Leave a reply



Submit