I am using the AKSwiftSlideMenu project from the web and trying to put an activity indicator so it will display the indicator when the user pressed the menu bars on the top right. I tried everything. is there a way once the user pressed the 3menubar at the top to display a activity indicator until the menu appear from the side.
I do some database processing to build the menu item before the slide out.
Since AKSwiftSlideMenu is using UINavigationController, you can just add UIActivityIndicator on the right side of the navigation controller.
Something like that:
func showActivityIndicator() {
let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let barButton = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.setRightBarButton(barButton, animated: true)
activityIndicator.startAnimating()
}
Don't forget, you need to prevent the Segue from happening, and halt the program until you are done loading.
Related
Background: I am creating a button with an image and a label. I add the label to the button with button.addSubview(labelName) and then I add the button to the main view with self.view.addsubview(buttonName). I am creating all views programatically and with UIKit. I do not use SwiftUI or Storyboard.
Problem: When pressing the button and holding the press, by default the button image automatically gets darker and loses color (I do not know what this animation or action is called, but I guess it's some kind of default button press/click action). The gif below shows this animation.
The problem is, only the background image of the button is affected. The label and the label background, that I added to the button, are not effected by this dim animation.
What I want to achieve: I want the label and the label background to get the same dim effect, when the button is pressed. I don't like the way the label stands out with its bright colors while the button is pressed. Do I need to recognize the click and add some kind of dim animation to all childviews of the button manually or should I avoid adding labels to buttons like I did in the code below?
Simplified code for testing:
func doStuff(){
let testbutton = UIButton(frame: CGRect(x: self.view.frame.width/4, y: 100, width: 200, height: 130))
let testImg = UIImage(named: "rand.jpeg")
testbutton.setImage(testImg, for: .normal)
testbutton.addTarget(self, action: #selector(otherStuff), for: .touchUpInside)
let label = UILabel(frame: CGRect(x: testbutton.bounds.origin.x+20, y: testbutton.bounds.origin.y+20, width: 150, height: 50))
label.text = "NOT EFFECTED"
label.backgroundColor = .red
testbutton.addSubview(label)
self.view.addSubview(testbutton)
}
I want to show something when the dial pop up view shown like the screen capture, please how can I do with Swift?
I didn't quite get that question but if you wanna do something when you keyboard pops up you can do it in searchBarTextDidBeginEditing() which is an delegate method for UISearchBar
https://developer.apple.com/documentation/uikit/uisearchbardelegate/1624303-searchbartextdidbeginediting
if you wanna know more about custom UIAlert than refer this YouTube video
https://www.youtube.com/watch?v=u6TpJrZNuPU
if You wanna present something when your actionSheet is presented than you can directly add the animation programmatically after presenting the Alert
for example I presented a view after presenting an UIAlerActionSheet
self.present(alert, animated: true)
let view1 = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
view1.backgroundColor = UIColor.red
view.addSubview(view1)
here view1 is presented and when needed you can remove that view1 from its superView so that it will vanish
view1.removeFromSuperview()
I have a toolbar that's instantiated on viewDidLoad on top of a webkit. When I take a snapshot on the simulator, the toolbar is missing which is what I would like. When built on the device, the toolbar is there.
I tried to hide the toolbar with:
toolbar.isHidden = true
but the application crashes with toolbar being nil. If I change it to:
toolbar?.isHidden = true
It still shows up considering it still thinks it's nil.
The toolbar is set up on viewDidLoad by calling another function:
var toolbar : UIToolbar!
override func viewDidLoad() {
super.viewDidLoad()
setUpToolBar()
}
func setUpToolBar() {
let saveButton = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(takeScreenshot))
...
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 300, width: 200, height: 50))
toolbar.setItems([saveButton,flexibleSpaceFillerLeft,userAgentButton,flexibleSpaceFillerRight,doneButton], animated: true)
view.addSubview(toolbar)
}
The code for my snapshot is below. This is where I tried to hide the toolbar before taking the snapshot.
#objc func takeScreenshot() {
webView.takeSnapshot(with: nil, completionHandler: { (image,error) in
if let image = image {
self.screenshotOfWindow = image
self.showScreenshotEffect()
self.saveAllData()
} else {
print (error?.localizedDescription as Any)
}
})
}
Here's the screen I need to take a screenshot of:
The red box in the screenshot is the bar that I need to disappear from the screenshot.
I'd like to be able to take the screenshot without the bottom bar in view. As stated before, this works in the simulator, but the device always shows the bar. There's also a "navigation controller" gap at the top of the screenshot since the top bar covers part of the screen at top, but this is just blank and something I can address later.
I just wanted to come back and answer how I solved this. The webview is embedded in a navigation controller, but I was creating the toolbar programmatically on viewDidLoad by calling a setupToolBar function I had created early on in the project. I could hide the toolbar, but it was still being captured while taking the screenshot. I commented all of that code out and used the navigation controller's toolbar instead. Now when I take the screenshot, the bottom and top bar of the navigation controller is not part of the screenshot.
I've removed the back button's text by manually setting it to " " on each navigation item, however there is still extra padding between the button and the navigation item's title for no reason.
Does anyone know how to get rid of this annoying spacing?
In a few real case scenarios in my app, the title does concatenate as it gets slightly too long, even though it wouldn't need to if that space were not there.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
let arrayViews = (self.navigationController?.navigationBar.subviews)
if let itemView = arrayViews?[1] {
for lbl in itemView.subviews {
lbl.frame = CGRect(x: -25, y: lbl.frame.origin.y, width: lbl.frame.size.width, height: lbl.frame.size.height)
}
}
}
You should create a custom UIBarButtonItem which uses popToViewController to go back to the previous item on your stack. That way, you can manually set the frame of your custom back button.
I want to mimic Instagram's tabbar functionality, where the middle option acts as a button that triggers a modal slide up segue instead of switching views like a normal tabbar button.
I don't know the correct way of doing this, but I'm trying to place a button directly over the middle tabbar button, but when pressing it it triggers the tabbar button instead of the normal button.
let count = CGFloat(tabBar.items!.count)
let itemSize = CGSize(width: tabBar.frame.size.width / count, height: tabBar.frame.height)
for (index, _) in tabBar.items!.enumerate() {
if index == 2 {
let xPosition = itemSize.width * CGFloat(index)
let backgroundButton = UIButton(frame: CGRect.init(x: xPosition, y: 0, width: itemSize.width, height: itemSize.height))
backgroundButton.backgroundColor = UIColor.redColor()
backgroundButton.addTarget(self, action: #selector(MainTabBarController.testButton), forControlEvents: .TouchUpInside)
//testButton just triggers a print statement for now
tabBar.insertSubview(backgroundButton, atIndex: 1)
}
}
When I press the middle button, it just triggers the normal view switching like with any other tabbar button, and does not print anything out meaning the overlayed button is never trigger.
Is there somewhere I went wrong, or better yet is there a better way of doing this? Prehaps making the tabbar button itself trigger a modal segue instead of the typical tabbar button behavior?