I need a floating button in my iPhone app, I saw many libraries online like this one: https://cocoapods.org/pods/LiquidFloatingActionButton
But I need it to be just a single button, not a menu - Do you know of any other libraries that do this? Or a way to customise one of the current libraries to do what I need?
Thanks!
import UIKit
final class AppDelegate: NSObject, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
let buttonDiameter: CGFloat = 44.0
let floatingButton = UIButton(frame: CGRect(x: 0.0, y: 0.0, width: buttonDiameter, height: buttonDiameter))
floatingButton.layer.cornerRadius = buttonDiameter / 2.0
floatingButton.backgroundColor = .redColor()
window?.addSubview(floatingButton)
return true
}
}
This example would add a floating button to the top left corner of your app.
The important aspect of this code is that I am adding the button the UIWindow.
You can access the window of the app from any UIViewController whose view is added to the window via the property window.
To get notifications when a view controller is added to the window you can override the function:
didMoveToWindow()
There is an approach to this by using KCFloating action button and adding a tapGestureRecognizer to a view containing this button.
So, just adding KCFloatingActionButton's pod and adding another view containing it it's possible to have a single floating action button just as androids.
If you require more detail to solve this issue tell me and I'll try to help
Sample image
only copy and paste this code
override func viewDidAppear(_ animated: Bool) {
layoutFAB()
}
func layoutFAB() {
let item = KCFloatingActionButton()
item.buttonColor = UIColor(red: 188/255, green: 46/255, blue: 35/255, alpha: 1)
}
override func viewDidLoad() {
super.viewDidLoad()
let Fab = KCFloatingActionButton()
Fab.addItem("a", icon: UIImage(named: "a")){ item in
print("a")
}
}
self.view.addSubview(Fab)
}
Related
I am using this Github-Repo for a reavealing splash view.
I am calling it like this inside my AppDelegate:
let revealingSplashView = RevealingSplashView(iconImage: UIImage(named: "wIcon")!, iconInitialSize: CGSize(width: 120 * UIScreen.main.bounds.width / 414.0, height: 120 * UIScreen.main.bounds.width / 414.0), backgroundColor: .white)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
revealingSplashView.startAnimation()
window?.rootViewController?.view.addSubview(revealingSplashView)
return true
}
I am facing an issue where sometimes after the animation is finished, it is not instantly transitioning to the other ViewController but showing a blank white screen.
Here is a screen-video for a better understanding.
Does anyone have an idea what can cause that?
Or is there an easy way to implement it without this repo?
You calling the revealingSplashView inside AppDelegate didFinishLaunchingWithOptions. If you check the documentation they clearly state to call it in the viewDidLoad method of your viewController. Your view has not been loaded that's why you got the delay.
Check the documentation which you linked above:
import RevealingSplashView
override func viewDidLoad() {
super.viewDidLoad()
//Initialize a revealing Splash with with the iconImage, the initial size and the background color
let revealingSplashView = RevealingSplashView(iconImage: UIImage(named: "twitterLogo")!,iconInitialSize: CGSize(width: 70, height: 70), backgroundColor: UIColor(red:0.11, green:0.56, blue:0.95, alpha:1.0))
//Adds the revealing splash view as a sub view
self.view.addSubview(revealingSplashView)
I tried this code :
in viewWillAppear :
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
and :
#objc func keyboardWillAppear(notification:NSNotification) {
print("keyboard appear")
var info = notification.userInfo
let keyBoardSize = info![UIKeyboardFrameEndUserInfoKey] as! CGRect
scrollCreateEditContact.contentInset = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
scrollCreateEditContact.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, keyBoardSize.height, 0.0)
}
#objc func keyboardWillDisappear(notification:NSNotification) {
print("keyboard disappear")
scrollCreateEditContact.contentInset = UIEdgeInsets.zero
scrollCreateEditContact.scrollIndicatorInsets = UIEdgeInsets.zero
}
and result is :
what I want is that textfield did not covered by keyboard when keyboard appear like this :
That code only work on textfield that is not inside tableView.
But when I click textfield inside tableView and log it "keyboard appear" always detected.
What is the correct code for textfield inside tableView not covered by keyboard when keyboard appear?
Thats common behaviour, ios cannot adjust content above keyboard automatically, unlike android. My solution is, you can wrap all that views (photo, textfield, etc) inside tableView. And use TPKeyboardAvoiding library.
pod 'TPKeyboardAvoiding'
If you use storyboard, set tableView base-class to TPKeyboardAvoidingTableView.
The easiest way to handle this problem is installing pod for IQKeyboardManager:
Installation via cocoa pods:
pod 'IQKeyboardManagerSwift'
Usage:
import IQKeyboardManagerSwift
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
IQKeyboardManager.shared.enable = true
return true
}
}
For more information on IQKeyboardManager refer this link:
https://github.com/hackiftekhar/IQKeyboardManager
Here are the most common and used libraries for managing such behavior:
TPKeyboardAvoiding
IQKeyboardManager
You can install them via CocoaPods as well.
I would like to do something in the vein of having a box appear or a circle around the tab bar item that the user has selected other than just changing the color. Is this even possible and if so how? Thanks in advance.
There are various ways to achieve this. I will list down 3 of them below.
1) You can set selectionIndicatorImage like this in AppDelegate,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UITabBar.appearance().selectionIndicatorImage = #imageLiteral(resourceName: "boxImage")
return true
}
For the rest of 2 you have to create a subclass of UITabBarController like below and set this class in the storyboard for the tabBarController
class MyTabBarViewController: UITabBarController {
}
2) In this method you have to ask your designer to create the selected images with box. Set the unselected/normal and selected images like below in viewDidAppear and you are done.
3) Add an imageView in tabBar like below. In storyboard, you can assign tag for each tabBar item so that in the didSelect item callback, we will consider the tag as index of the selected item. Lets consider you have 5 tabBar items and you assigned tags from 0 to 4. Now you will just get the tag and update the position as shown below
class MyTabBarViewController: UITabBarController {
// MARK: - Properties
let imageView = UIImageView(image: #imageLiteral(resourceName: "boxImage"))
// MARK: View's Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.addSubview(imageView)
}
/// Tabbar item selection callback
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
updateImagePosition(at: item.tag + 1)
}
private func updateImagePosition(at index: Int) {
guard let count = tabBar.items?.count else { return }
let eachItemWidth = view.bounds.width/CGFloat(count)
let selectedItemX = (eachItemWidth * CGFloat(index)) - eachItemWidth/2 - imageView.frame.width/2
let selectedItemY = tabBar.bounds.height/2 - imageView.frame.height/2
imageView.frame.origin = CGPoint(x: selectedItemX, y: selectedItemY)
}
}
In XCode 7.3.x ill changed the background Color for my StatusBar with:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
But it seems that this is not working anymore with Swift 3.0.
Ill tried with:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
But it gives me:
this class is not key value coding-compliant for the key statusBar.
Any Ideas how to change it with XCode8/Swift 3.0?
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
Update for iOS 13
App called -statusBar or -statusBarWindow on UIApplication: this code
must be changed as there's no longer a status bar or status bar
window. Use the statusBarManager object on the window scene instead.
Refer to How to change the status bar background color and text color on iOS 13?
"Change" status bar background color:
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let statusBarColor = UIColor(red: 32/255, green: 149/255, blue: 215/255, alpha: 1.0)
statusBarView.backgroundColor = statusBarColor
view.addSubview(statusBarView)
Change status bar text color:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Update: please note that the status bar frame will change when the view is rotated. You could update the created subview frame by:
Using the autoresizing mask:
statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
Observing NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
Or overriding viewWillLayoutSubviews()
With using Swift 3 and 4 you can use the code snippet on below. It finds the view from UIApplication using valueForKeyPath as set it's background color.
guard let statusBarView = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else {
return
}
statusBarView.backgroundColor = UIColor.red
Objective-C
UIView *statusBarView = [UIApplication.sharedApplication valueForKeyPath:#"statusBarWindow.statusBar"];
if (statusBarView != nil)
{
statusBarView.backgroundColor = [UIColor redColor];
}
For Xcode 9 and iOS 11:
The style of the status bar we will try to achieve is a status bar with white content. Go to the ViewController.swift file and add the following lines of code.
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Or from project settings option you can change status bar's style:
Next, go back to the Storyboard, Select the View Controller and in the Editor menu Select Embed in Navigation Controller. Select the Navigation Bar and in the Attribute Inspector set the Bar Tint color to red. The Storyboard will look like this.
Build and Run the project, The content of the status bar is dark again, which is the default. The reason for this is, iOS asked for the style of the status bar of the navigation controller instead of the contained view controller.
To change the style of all navigation controller inside the app, change the following method in the AppDelegate.swift file.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}
Build and Run the Project again, this time the content of the status bar changed to white.
I have a custom solution for changing status bar on iOS 13 and below. Here is how to do that:
if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView()
statusbarView.backgroundColor = UIColor.red
view.addSubview(statusbarView)
statusbarView.translatesAutoresizingMaskIntoConstraints = false
statusbarView.heightAnchor
.constraint(equalToConstant: statusBarHeight).isActive = true
statusbarView.widthAnchor
.constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
statusbarView.topAnchor
.constraint(equalTo: view.topAnchor).isActive = true
statusbarView.centerXAnchor
.constraint(equalTo: view.centerXAnchor).isActive = true
} else {
let statusBar = UIApplication.shared.value(forKeyPath:
"statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
}
Gist
Also, check the article iOS 13 How to Change StatusBar Color?
One last thing, you can still change statusbar style with :
override var preferredStatusBarStyle : UIStatusBarStyle {
return UIStatusBarStyle.lightContent
//return UIStatusBarStyle.default // Make dark again
}
For iOS 11 and Xcode 9 use the following steps.
create an extention to UIApplication class:
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
In your class or wherever you want to change the Status bar's background color:
UIApplication.shared.statusBarView?.backgroundColor = .red
For light content or dark content of status bar simply go to Info.plist and add the following value row with value NO.
View controller-based status bar appearance
Now just set the light content or whatever you need in the General Tab of your project's settings.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any] ?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}
This works for me, as my navigation barTintColor was black and unable to see the status bar.
When set above code it didFinishLaunch status bar appears in white.
write this in first view controller:
UIApplication.shared.statusBarUIView?.backgroundColor = .white
extension UIApplication {
var statusBarUIView: UIView? {
if #available(iOS 13.0, *) {
let tag = 38482458385
if let statusBar = self.keyWindow?.viewWithTag(tag) {
return statusBar
} else {
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
statusBarView.tag = tag
self.keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil
}
}
I made this extension to change color of status bar. It's not dependent on the key.
So it is much safer to use
public extension UIViewController {
func setStatusBar(color: UIColor) {
let tag = 12321
if let taggedView = self.view.viewWithTag(tag){
taggedView.removeFromSuperview()
}
let overView = UIView()
overView.frame = UIApplication.shared.statusBarFrame
overView.backgroundColor = color
overView.tag = tag
self.view.addSubview(overView)
}
}
Here is usage anywhere in viewcontroller:
setStatusBar(color: .red)
Try this
Goto your app info.plist
Set View controller-based status bar appearance to NO
Set Status bar style to UIStatusBarStyleLightContent
Then Goto your app delegate and paste the following code where you set your Windows's RootViewController.
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0"))
{
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
view.backgroundColor=[UIColor blackColor];
[self.window.rootViewController.view addSubview:view];
}
Previous Code:-
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
My application got a crash show reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'
Updated Code-
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = UIColor.init(red: 237.0/255.0, green: 85.0/255.0, blue: 61.0/255.0, alpha: 1.0)
UIApplication.shared.keyWindow?.addSubview(statusBar)
} else {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.init(red: 237.0/255.0, green: 85.0/255.0, blue: 61.0/255.0, alpha: 1.0)
}
This code is working swift 5.2
enter image description here
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
The possible solution is adding view that will be used as background of statusBar on your viewController:
let frame = screenController.view.convert(UIApplication.shared.statusBarFrame, to: screenController.view)
let backview = UIView(frame: frame)
backview.backgroundColor = backgroundColor
screenController.view.addSubview(backview)
screenController.view.bringSubviewToFront(backview)
Add following code in your extension file to edit your status bar in swift 4 and Above:
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
now, we can edit status bar by adding following line in our ViewController class:
UIApplication.shared.statusBarView?.backgroundColor = <Your Color name>
ex: UIApplication.shared.statusBarView?.backgroundColor = .red
Hope, this will be helpful.
Thanks
IOS 15, Xcode 13.2.1, Swift 5
I was able to get this to work without errors or warnings using the following:
func statusBarColor() {
if #available(iOS 13.0, *) {
let statusBar2 = UIView()
if UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame != nil {
statusBar2.frame = (UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame)!
statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
UIApplication.shared.windows.first?.addSubview(statusBar2)
}
} else {
let statusBar2: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
}
}
Use: Call the function in viewDidLoad for single scene applications or applications with only a single statusBar color change needed. For applications with multiple scenes calling for different statusBar colors I recommend calling the function in viewWillAppear.
Xcode allows to create launch screen in .xib files via Interface Builder. Is it possible to execute some code with the xib, just like in usual view controllers? It would be great if we can set different text/images/etc while app launching.
No, it's not possible.
When launch screen is being displayed your app will be in loading state.
Even the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions will not be completely executed while the launch screen is displayed.
So it's clear that, you don't have any access to your app and so at this point you can't execute any code.
I was trying to do the same thing here. :)
I really liked some of the apps, in which they do a little dynamic greeting text and image each time the app is launched, such as "You look good today!", "Today is Friday, a wonderful day", etc, which is very cute.
I did some search, below is how to do it:
(My code is XCode 7, with a launchscreen.xib file)
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var customizedLaunchScreenView: UIView?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
application.statusBarHidden = true
// customized launch screen
if let window = self.window {
self.customizedLaunchScreenView = UIView(frame: window.bounds)
self.customizedLaunchScreenView?.backgroundColor = UIColor.greenColor()
self.window?.makeKeyAndVisible()
self.window?.addSubview(self.customizedLaunchScreenView!)
self.window?.bringSubviewToFront(self.customizedLaunchScreenView!)
UIView.animateWithDuration(1, delay: 2, options: .CurveEaseOut,
animations: { () -> Void in
self.customizedLaunchScreenView?.alpha = 0 },
completion: { _ in
self.customizedLaunchScreenView?.removeFromSuperview() })
}
return true
}
// other stuff ...
}
Just do what ever you wanted to show, text, images, animations, etc. inside the customizedLaunchScreenView here.
At the end of the launching, just fade out this customized UIView using alpha value change, then remove it completely.
How cool is that? I absolutely love it!
Hope it helps.
I was also trying to achieve this. I tried the following, it gives a delay of couple of seconds but works for me.
Create and set a launch screen in project settings.
Create a view controller with custom class (SplashViewController) and set it your starting view controller in storyboard.
Add a container view in it and set it to full screen.
Set embedded segue to a Storyboard Reference.
Select Storyboard Reference and set launch screen in StoryBoard property from Attribute inspector.
Do whatever you want in SplashViewController (play animation or session check etc) and perform segue when done.
Hope it helps!
Language: Swift 4
Hello, here's a solution for using xib for launch screen.
Adding new class named LaunchView to your project, then u'll have a new file LaunchView.swift in project.
Go to LauchImage.xib to set class to LaunchView (which it's your new class).
Adding some code, & reading some information show at the LaunchView.swift. Here's is my code for displaying version of app at launch screen.
class LaunchView: UIView {
lazy var versionLabel: UILabel = {
let label = Factory.label(style: LabelStyle.custom(font: .regular, size: 10, color: .other(ColorUtility.versionGray)), mutiLine: false, textAlignment: .center)
label.text = "ver \(Constants.appVersion)"
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
nibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
nibSetup()
}
private func nibSetup() {
addSubview(versionLabel)
versionLabel.snp.makeConstraints { (make) in
make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom).offset(-6)
make.centerX.equalToSuperview()
}
}
}