Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there an option to observe and get the information about appearing and disappearing?
I want to grayscale my elements like apple ui-elements by appearing of UIAlertController!
Since now i found out that the "_UIBackdropViewComputeAndApplySettingsNotification" was called and contains userInfo about the appearing view.
You are going to make the UIAlertController's view appear, so how can you not know? You don't need to observe it; you're doing it (by calling presentViewController...).
That takes of what happens when the alert appears. What about when it disappears? Well, it disappears because the user tapped a button. You get to write the handler for every button in the alert. So again, you know when the alert is disappearing, because your handler is running.
The solution is, everything works automatically. You just have to implement..
override func tintColorDidChange() {
self.setNeedsDisplay()
}
..and of course working with the tintColor
Thanks matt for quick answer!
To expand on the other answers: each of your UIView subclasses should implement tintColorDidChange to be notified of the change.
Here's a sample implementation:
class someLabel : UILabel {
override func tintColorDidChange() {
let isInactive = self.tintAdjustmentMode == UIViewTintAdjustmentMode.Dimmed
if (isInactive) {
// modify subviews to look disabled
self.textColor = UIColor.grayColor()
} else {
// modify subviews to look enabled
self.textColor = self.tintColor
}
}
}
A few other good code samples (albeit in Objective-C) can be found in this SO question.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 13 hours ago.
Improve this question
I want to implement a button that inverts the background color of the app when the user presses it. I tried assigning dark and light to the overrideUserInterfaceStyle property, but it only changes the color of the navigation bar. The only answers I've found so far are to create an ancestor controller of the full controller and manage the colors globally there. Can you tell me if there is a property or method that can access the full colors of the app? Or is there another good way...
here's my code I tried but only get feature toggles color of navigation bar dark/light...
#objc func onClickSwitch(_ sender : UISwitch) {
if #available(iOS 13.0, *) {
let windowScene = UIApplication.shared.connectedScenes.first as! UIWindowScene
if sender.isOn {
print("dark mode")
windowScene.keyWindow?.overrideUserInterfaceStyle = .dark
} else {
print("white mode")
windowScene.keyWindow?.overrideUserInterfaceStyle = .light
}
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to refresh the label in a UITableViewCell without having to refresh the entire table. This is for making a timer-app, where I will be refreshing the labels in the UITableView every second (when it's counting down).
Link to a simulator screenshot of the table view
In the past, I've tried reloading the data of the entire UITableView five times every second, but this results in sliding to delete being very inconsistent and hard to perform. I haven't succeeded in updating individual rows yet, so I don't know if that's a possible solution.
I couldn't find any similar question, but if you know that it's been asked before, please redirect me.
Any help would be greatly appreciated!
Thanks!
The most efficient way in Swift is a callback closure. It causes much less overhead than protocol/delegate or notification.
Run the timers in the data model.
In the model declare a callback property
var updateLabel : ((String) -> Void)?
and call it when the timer fires
#objc func timerDidFire(_ sender: Timer) {
updateLabel?("New Value") // replace "New Value" with the actual value
}
In cellForRowAt assign a closure to the updateLabel property to update the label
let model = datasourceArray[indexPath.row]
model.updateLabel = { value in
cell.textLabel.text = value
}
In tableView:didEndDisplaying:forRowAt: remove the closure
let model = datasourceArray[indexPath.row]
model.updateLabel = nil
If you can find out index of the respective label then you can easily get the cell and access the label and do what ever you want to do
let indexPath = IndexPath(row: 2, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? YourTableViewCell {
cell.YourLabel.backgroundColor = UIColor.green // write as per your requirement
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In my Swift project, ViewController #1 has a route of destinations listed in a table. User is driving along and we keep track of where user is using the built-in CLLocation stuff. But user has now navigated away from ViewController #1 to some other portion of the app and is now in ViewController #2. A certain event that I track in ViewController #1 fires (in my example; the user has reached the next destination on his list). I need the app to immediately segue back to ViewController #1 right when that event fires. How can it be sensed when user is in ViewController #2?
NSNotificationCenter and an Unwind Segue seems to be the way to go, this way this functionality can be reused, all you would need to do is register for a given notification on the relevant view controllers.
Something along the lines of:
class ViewControllerOne: UIViewController {
let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"
//- your event happens
SomethingHappened {
NSNotificationCenter.defaultCenter().postNotification(CallForUnwindSegue, object: nil)
}
}
class ViewControllerTwo: UIViewController {
let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"
override func viewDidLoad() {
super.viewDidLoad()
//- Subscribe for notifications
NSNotificationCenter.defaultCenter().addObserver(self, #selector(self.doSomething), name: CallForUnwindSegue)
}
//- Unsubscribe for notifications
override func deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func doSomething(notification: NSNotification) {
//Handle your event here.
}
}
There's a good unwind segue article here
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Strange behaviour observed on UITextField text disappears,
Steps:
Type text in UITextField and click home button.
Open another application (App) and click home button.
Then Open First App the UITextField in text is disappears.
Issue observed in IPhone 6s version 9.2.1. but perfectly working in simulator.
You can override this method in your custom responders to update your object's state or perform some action such as highlighting the selection. If you override this method, you must call super at some point in your implementation.
The custom Class should be similar to:
class CustomSecureTextField: UITextField {
override func becomeFirstResponder() -> Bool {
super.becomeFirstResponder()
//if !isSecureTextEntry {
// return true
//}
if let currentText = text {
insertText(currentText)
}
return true
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to handle the pressing of the back button on a UI Navigation bar. I have an extension in objective C (https://github.com/onegray/UIViewController-BackButtonHandler) and i have bridged it to my project in Swift using the header, now I do not quite know how to implement the code in Swift. This is the implementation in C:
-(BOOL) navigationShouldPopOnBackButton {
if(needsShowConfirmation) {
// Show confirmation alert
// ...
return NO; // Ignore 'Back' button this time
}
return YES; // Process 'Back' button click and Pop view controler
}
func navigationShouldPopOnBackButton() -> Bool {
if(needsShowConfirmation) {
// Show confirmation alert
// ...
return false // Ignore 'Back' button this time
}
return true // Process 'Back' button click and Pop view controller
}