Hide add button - swift

I am trying to have a button, so the user can hide the add banner if they want to, this works but when the add banner is not displaying I want the button to also be hidden. So I added the following code, but as soon as I did so, the button stopped working. Any ideas as to why?
thank you!
override func viewDidLoad() {
super.viewDidLoad()
if Banner.hidden == false{
hideAdd.hidden = false
} else {
hideAdd.hidden = true
}
The following is the code I used to hide the banner
#IBAction func HideAddButton(sender: AnyObject) {
Banner.hidden = true
}

The easiest way to hide and unhide objects on a MainStoryboard is to change the alpha to 0 in the attributes inspector. Then when you want it to appear you can just change the alpha in your code to 1 when you want it to appear in ViewController.swift. Or Vice Versa.

Related

Disable closeButton in 2nd ViewController

Im trying to disable the closeButton in my second ViewController "OptionsVC"
Here's what I tried:
self.view.window!.standardWindowButton(NSWindowButton.closeButton)!.isHidden = true
Did that, got this:
EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0
Also tried
var button = view.window?.standardWindowButton(NSWindowButton.ZoomButton)
button?.isEnabled = false
No changes
In order to disable a button, it should be initialized. Therefore, you will face problems disabling the button from another view controller. Try adding a boolean and on viewDidLoad() check the boolean and decide either to enable or disable the button.
I got it.
override func viewDidAppear(){
super.viewDidAppear()
if let window1 = self.view.window
{
window1.styleMask.remove( [.closable, .resizable] )
}
}
This solved my problem

How to Disable Multi Touch on UIBarButtonItems & UI Buttons?

My app has quite a few buttons on each screen as well as a UIBarButtonItem back button and I have problems with people being able to multi click buttons. I need only 1 button to be clickable at a time.
Does anyone know how to make a UIBarButtonItem back button exclusive to touch?
I've managed to disable multi clicking the UIButtons by setting each one's view to isExclusiveTouch = true but this doesn't seem to count for the back button in the navigation bar.
The back button doesn't seem to adhere to isExclusiveTouch.
Does anyone have a simple work around that doesn't involve coding each and every buttons send events?
Many Thanks,
Krivvenz.
you can enable exclusive touch simply this will stop multiple touch until first touch is not done
buttton.exclusiveTouch = true
You could write an extension for UIBarButtonItem to add isExclusiveTouch?
you can simply disable the multi-touch property of the super view. You can also find this property in the storyboard.
you could try this for the scene where you want to disable multiple touch.
let skView = self.view as! SKView
skView.isMultipleTouchEnabled = false
I have found a solution to this. isExclusiveTouch is the solution in the end, but the reason why this property didn't do anything is because you need to set it also on all of the subviews of each button that you want to set as isExclusiveTouch = true. Then it works as expected :)
It's working fine in Swift
self.view.isMultipleTouchEnabled = false
buttonHistory.isExclusiveTouch = true
In addition of #Luky LĂ­zal answer, you need pay attention that all subviews of a view you want disable multi-touching (exactly all in hierarchy, actually subviews of subviews) must be set as isExclusiveTouch = true.
You can run through all of them recursively like that:
extension UIView
{
func allSubViews() -> [UIView] {
var all: [UIView] = []
func getSubview(view: UIView) {
all.append(view)
guard view.subviews.count > 0 else { return }
view.subviews.forEach{ getSubview(view: $0) }
}
getSubview(view: self)
return all
}
}
// Call this method when all views in your parent view were set
func disableMultipleTouching() {
self.isMultipleTouchEnabled = false
self.allSubViews().forEach { $0.isExclusiveTouch = true }
}

Swapping bar button items

I'd like to swap a bar button item, depending on a UISegmentControl which I thought would be fairly straight forward.
My initial plan was:
Create an IBAction for when the segment changes
Work out which segment we are on
Hide the appropriate button
The code was something like this
#IBAction func segmentChanged(sender: UISegmentedControl) {
UIView.animateWithDuration(0.25) { () -> Void in
if(sender.selectedSegmentIndex == 0) {
barButtonA.hidden = true
barButtonB.hidden = false
} else {
barButtonA.hidden = false
barButtonB.hidden = true
}
}
}
That hid the right button as expected, but oddly, the space where the button was still remained, so it looked really strange.
Next I saw that in the interface builder there is a parent container (UIBarButtonItems) for each of the buttons. I'm not sure why they're needed, but I hooked them up via IBOutlets so I could hide/show them in the function, but that didn't work either as they don't have a hidden property.
Finally, I tried to use those new outlets with following code:
#IBAction func segmentChanged(sender: UISegmentedControl) {
UIView.animateWithDuration(0.25) { () -> Void in
if(sender.selectedSegmentIndex == 0) {
self.navigationItem.rightBarButtonItem = self.barButtonItemA
} else {
self.navigationItem.rightBarButtonItem = self.barButtonItemB
}
}
}
But no luck! Here, it just seems to animate swapping the two button positions/order, rather than actually removing either one.
What am I doing wrong? Thanks!
Yo should combine the two function you have.
So swap them and change the hidden state.
Update:
Did you tried to first delete the navigation.barbutton.
self.navigationItem.rightBarButtonItem = nil

(swift) Anyone can help me to make bulk change to those buttons?

here is the screenshot
when user get into this page, I want to change the background of those button.
for example, when the level of user is 6, I want to change the top 6 background of buttons to a blue picture as first button. I don't wanna write a code one by one, so please help me to write a function, Thank you very much!
You can add you buttons to the array in order of increasing level and do something like this:
var buttons: [UIButton] // array of your buttons, next you need to init it
...
func setColorForUserLevel(userLevel: Int) {
if userLevel > buttons.count {
return
} else {
for i in 0...userLevel - 1 {
buttons[i].backgroundColor = UIColor.blueColor()
}
}
}
I create simple example project to show how to use it:
I have outlets with UIButton! type and buttons object in code:
And i test setColorForUserLevel function in viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
buttons = [button1, button2, button3]
setColorForUserLevel(2)
}

Disable a Button

I want to disable a button (UIButton) on iOS after it is clicked. I am new to developing for iOS but I think the equivalent code on objective - C is this:
button.enabled = NO;
But I couldn't do that on swift.
The boolean value for NO in Swift is false.
button.isEnabled = false
should do it.
Here is the Swift documentation for UIControl's isEnabled property.
If you want the button to stay static without the "pressed" appearance:
// Swift 2
editButton.userInteractionEnabled = false
// Swift 3
editButton.isUserInteractionEnabled = false
Remember:
1) Your IBOutlet is --> #IBOutlet weak var editButton: UIButton!
2) Code above goes in viewWillAppear
The way I do this is as follows:
#IBAction func pressButton(sender: AnyObject) {
var disableMyButton = sender as? UIButton
disableMyButton.enabled = false
}
The IBAction is connected to your button in the storyboard.
If you have your button setup as an Outlet:
#IBOutlet weak var myButton: UIButton!
Then you can access the enabled properties by using the . notation on the button name:
myButton.enabled = false
Disable a button on Swift 3:
yourButton.isEnabled = false
For those who Googled "disable a button" but may have more nuanced use cases:
Disable with visual effect: As others have said, this will prevent the button from being pressed and the system will automatically make it look disabled:
yourButton.isEnabled = false
Disable without visual effect: Are you using a button in a case where it should look normal but not behave likes button by reacting to touches? Try this!
yourButton.userInteractionEnabled = false
Hide without disabling: This approach hides the button without disabling it (invisible but can still be tapped):
yourButton.alpha = 0.0
Remove: This will remove the view entirely:
yourButton.removeFromSuperView()
Tap something behind a button: Have two buttons stacked and you want the top button to temporarily act like it's not there? If you won't need the top button again, remove it. If you will need it again, try condensing its height or width to 0!
Swift 5 / SwiftUI
Nowadays it's done like this.
Button(action: action) {
Text(buttonLabel)
}
.disabled(!isEnabled)
You can enable/disable a button using isEnabled or isUserInteractionEnabled property.
The difference between two is :
isEnabled is a property of UIControl (super class of UIButton) and it has visual effects (i.e. grayed out) of enable/disable
isUserInteractionEnabled is a property of UIView (super class of UIControl) and has no visual effect although but achieves the purpose
Usage :
myButton.isEnabled = false // Recommended approach
myButton.isUserInteractionEnabled = false // Alternative approach
Let's say in Swift 4 you have a button set up for a segue as an IBAction like this #IBAction func nextLevel(_ sender: UIButton) {}
and you have other actions occurring within your app (i.e. a timer, gamePlay, etc.). Rather than disabling the segue button, you might want to give your user the option to use that segue while the other actions are still occurring and WITHOUT CRASHING THE APP. Here's how:
var appMode = 0
#IBAction func mySegue(_ sender: UIButton) {
if appMode == 1 { // avoid crash if button pressed during other app actions and/or conditions
let conflictingAction = sender as UIButton
conflictingAction.isEnabled = false
}
}
Please note that you will likely have other conditions within if appMode == 0 and/or if appMode == 1 that will still occur and NOT conflict with the mySegue button. Thus, AVOIDING A CRASH.
The button can be Disabled in Swift 4 by the code
#IBAction func yourButtonMethodname(sender: UIButon) {
yourButton.isEnabled = false
}
in order for this to work:
yourButton.isEnabled = false
you need to create an outlet in addition to your UI button.
Building on other answers here. I wanted to disable button for a few seconds to prevent double taps. Swift 5 version, xcode 13.4.1 likes this and has no warnings or errors.
#IBAction func saveComponent(_ sender: Any) {
let myButton = sender as? UIButton
myButton?.isEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(2000))
{
myButton?.isEnabled = true
}
}