AZTabBarController doesn't stick to bottom - swift

I'm trying to implement AZTabBarController within my project but I am struggling to fix the bar to the bottom of the screen. My assumption is that the bar can be fixed to the bottom using some method or that I have some element that is pushing the bar some 50pts higher. Perhaps there is a margin of some sort that keeps the bar from staying at the bottom. Any suggestions will be much appreciated.
Here is my AZTabBar code. I've borrowed it from the example within the repo.
class CustomTabBarController: UITabBarController {
var tabController: AZTabBarController!
override func viewDidLoad() {
super.viewDidLoad()
var icons = [UIImage]()
icons.append(UIImage.init(named: "ic_home")!)
icons.append(UIImage.init(named: "ic_new")!)
icons.append(UIImage.init(named: "ic_settings")!)
//The icons that will be displayed for each tab once they are selected.
var selectedIcons = [UIImage]()
selectedIcons.append(UIImage.init(named: "ic_home")!)
selectedIcons.append(UIImage.init(named: "ic_new")!)
selectedIcons.append(UIImage.init(named: "ic_settings")!)
tabController = AZTabBarController.insert(into: self, withTabIcons: icons, andSelectedIcons: selectedIcons)
tabController.delegate = self
tabController.setViewController(QueryViewController.instance(), atIndex: 0)
tabController.setViewController(SettingsViewController.instance(), atIndex: 2)
//customize
let color = UIColor(red: 14.0/255, green: 122.0/255, blue: 254.0/255, alpha: 1.0)
tabController.selectedColor = color
tabController.highlightColor = color
tabController.highlightedBackgroundColor = #colorLiteral(red: 0.1803921569, green: 0.8, blue: 0.4431372549, alpha: 1)
tabController.defaultColor = .lightGray
//tabController.highlightButton(atIndex: 2)
tabController.buttonsBackgroundColor = UIColor(red: (247.0/255), green: (247.0/255), blue: (247.0/255), alpha: 1.0)//#colorLiteral(red: 0.2039215686, green: 0.2862745098, blue: 0.368627451, alpha: 1)
tabController.selectionIndicatorHeight = 0
tabController.selectionIndicatorColor = color
tabController.tabBarHeight = 60
tabController.setAction(atIndex: 0) {
//Your statments
print("Home!")
}
tabController.setAction(atIndex: 1) {
//Your statments
print("NEW Situation!")
}
tabController.setAction(atIndex: 2) {
//Your statments
print("Settings")
}
tabController.animateTabChange = false
tabController.onlyShowTextForSelectedButtons = false
tabController.setTitle("Home", atIndex: 0)
tabController.setTitle("New", atIndex: 1)
tabController.setTitle("Settings", atIndex: 2)
//tabController.font = UIFont(name: "AvenirNext-Regular", size: 12)
}
override var childForStatusBarStyle: UIViewController?{
return tabController
}
func getNavigationController(root: UIViewController)->UINavigationController{
let navigationController = UINavigationController(rootViewController: root)
navigationController.title = title
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.barStyle = .black
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.barTintColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
return navigationController
}
}

Sub classing
class CustomTabBarController: UITabBarController {
is the reason you should use AZTabBarController directly , what you see as margin in bottom is the parent class's tabBar which is empty because you don't set any viewControllers for it

Related

Edit collectionView Cells in UICollectionView

I am creating an app programitcally (on my first try). I have managed to create a collection view and on the click of the add button in the navigation bar a new cell appears. I want to be able to set all cells created to be editable (in other words show the delete button on the cells) when the edit button in the navigation bar is clicked. Currently no matter what I try whether I click on edit or not, nothing happens. I have tried hiding the UIImage of the button as well as the button by calling a function created in the cell class when the edit button is clicked and have also tried moving this function to the ViewController Class, app runs but code doesn't do what it is supposed to.
My Code:
class CollectionViewController: UICollectionViewController, UITextViewDelegate {
// decelrations of variables for use
let cell = ListCell()
let cellId = "cellId"
var numberOfLists = 0
var lists = [String]()
let secondVC = TableViewController()
// for learning purposes: codes sets up the view programitcally along with what is in scenedelagate
override func viewDidLoad() {
super.viewDidLoad()
setCollectionView()
}
//Function that will generate the collectionView
func setCollectionView() {
collectionView.register(ListCell.self, forCellWithReuseIdentifier: cellId)
collectionView.backgroundColor = .white
navigationItem.title = "Lists"
navigationController?.navigationBar.barTintColor = UIColor(white: 200/255, alpha: 1)
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont.boldSystemFont(ofSize: 20)]
navigationItem.leftBarButtonItem = editButtonItem
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
collectionView.delegate = self //runs the delegate flow methods
collectionView.dataSource = self
}
// to do with enabling editing in CV - function tied to edit button
override func setEditing(_ editing: Bool, animated: Bool){
super.setEditing(editing, animated: animated)
if editing {
print ("Editing Mode Enabled")
//cell.showButton()
} else {
print ("Editing Mode Closed")
}
// reloads the view after code is performed
self.collectionView.reloadData()
}
// func when buttons are tapped to add a collection view
#objc func addTapped() {
print("This button should not crash")
numberOfLists += 1
navigationController?.pushViewController(secondVC, animated: true)
collectionView.reloadData()
}
}
// the following block of code has to do with the set up of the collectionView
extension CollectionViewController: UICollectionViewDelegateFlowLayout {
// this code should speficiy how many cells you are going to have in your collectionview
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfLists
}
// this code lets you reuse a cell
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)-> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ListCell
cell.deleteButton.setImage(UIImage.init(named: "delete"), for: .normal)
return cell
}
// this code sets the sizing of the cells
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (view.frame.width / 2) - 16, height: 100)
}
// where to place the cell on the screen
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 8, left: 9, bottom: 8, right: 8)
}
// this function just checks to see if the cell is selected
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
navigationController?.pushViewController(secondVC, animated: true)
}
}
//this class sets up the collectionViewCell
class ListCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupCell()
}
fileprivate func setupCell(){
let colors = cellRandomBackgroundColors()
self.backgroundColor = colors[0]
self.addSubview(listNameLabel)
roundCorner()
setCellShadow()
self.addSubview(deleteButton)
deleteButton.addTarget(self, action: #selector(deleteCell), for: .touchUpInside)
}
#objc func deleteCell() {
print ("this button works")
}
func showButton() {
deleteButton.isHidden = true
print ("this is supposed to work")
}
func roundCorner() {
self.contentView.layer.cornerRadius = 50.0
self.contentView.layer.masksToBounds = true
self.contentView.layer.borderWidth = 1.0
self.contentView.layer.borderColor = UIColor.clear.cgColor
}
func setCellShadow() {
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 1)
self.layer.shadowOpacity = 0.2
self.layer.shadowRadius = 1.0
self.layer.masksToBounds = false
self.layer.cornerRadius = 3
self.clipsToBounds = false
}
func cellRandomBackgroundColors() -> [UIColor] {
//Colors
let red = [#colorLiteral(red: 0.9654200673, green: 0.1590853035, blue: 0.2688751221, alpha: 1),#colorLiteral(red: 0.7559037805, green: 0.1139892414, blue: 0.1577021778, alpha: 1)]
let orangeRed = [#colorLiteral(red: 0.9338900447, green: 0.4315618277, blue: 0.2564975619, alpha: 1),#colorLiteral(red: 0.8518816233, green: 0.1738803983, blue: 0.01849062555, alpha: 1)]
let orange = [#colorLiteral(red: 0.9953531623, green: 0.54947716, blue: 0.1281470656, alpha: 1),#colorLiteral(red: 0.9409626126, green: 0.7209432721, blue: 0.1315650344, alpha: 1)]
let yellow = [#colorLiteral(red: 0.9409626126, green: 0.7209432721, blue: 0.1315650344, alpha: 1),#colorLiteral(red: 0.8931249976, green: 0.5340107679, blue: 0.08877573162, alpha: 1)]
let green = [#colorLiteral(red: 0.3796315193, green: 0.7958304286, blue: 0.2592983842, alpha: 1),#colorLiteral(red: 0.2060100436, green: 0.6006633639, blue: 0.09944178909, alpha: 1)]
let greenBlue = [#colorLiteral(red: 0.2761503458, green: 0.824685812, blue: 0.7065336704, alpha: 1),#colorLiteral(red: 0, green: 0.6422213912, blue: 0.568986237, alpha: 1)]
let kindaBlue = [#colorLiteral(red: 0.2494148612, green: 0.8105323911, blue: 0.8425348401, alpha: 1),#colorLiteral(red: 0, green: 0.6073564887, blue: 0.7661359906, alpha: 1)]
let skyBlue = [#colorLiteral(red: 0.3045541644, green: 0.6749247313, blue: 0.9517192245, alpha: 1),#colorLiteral(red: 0.008423916064, green: 0.4699558616, blue: 0.882807076, alpha: 1)]
let blue = [#colorLiteral(red: 0.1774400771, green: 0.466574192, blue: 0.8732826114, alpha: 1),#colorLiteral(red: 0.00491155684, green: 0.287129879, blue: 0.7411141396, alpha: 1)]
let bluePurple = [#colorLiteral(red: 0.4613699913, green: 0.3118675947, blue: 0.8906354308, alpha: 1),#colorLiteral(red: 0.3018293083, green: 0.1458326578, blue: 0.7334778905, alpha: 1)]
let purple = [#colorLiteral(red: 0.7080290914, green: 0.3073516488, blue: 0.8653779626, alpha: 1),#colorLiteral(red: 0.5031493902, green: 0.1100070402, blue: 0.6790940762, alpha: 1)]
let pink = [#colorLiteral(red: 0.9495453238, green: 0.4185881019, blue: 0.6859942079, alpha: 1),#colorLiteral(red: 0.8123683333, green: 0.1657164991, blue: 0.5003474355, alpha: 1)]
let colorsTable: [Int: [UIColor]] = [0: red, 1: orangeRed, 2: orange, 3: yellow, 4: green, 5: greenBlue, 6: kindaBlue, 7: skyBlue, 8: blue, 9: bluePurple, 10: bluePurple, 11: purple, 12: pink]
let randomColors = colorsTable.values.randomElement()
return randomColors!
}
let deleteButton: UIButton = {
let deleteButton = UIButton(frame: CGRect(x:2,y:2,width:70,height:30))
return deleteButton
}()
let listNameLabel: UILabel = {
let listLabel = UILabel(frame: CGRect(x:2,y:50,width:70,height:30))
listLabel.text = "Data entry on Second View"
listLabel.font = UIFont.boldSystemFont(ofSize: 12)
listLabel.backgroundColor = .green
listLabel.translatesAutoresizingMaskIntoConstraints = false
listLabel.tag = 1
return listLabel
}()
let iconImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "Folder")
imageView.contentMode = .scaleAspectFit
return imageView
}()
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Edit Button on Cells that doesn't disappear

IOS13 Status Bar Background Color doesn't change to transparent after being set

(IOS13) I am trying to implement the functionality that shows the navigation bar and status bar gradually after scrolling down the page (with the following code):
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
var offset = scrollView.contentOffset.y / 150
print(offset)
if offset > 1 { // show navigation bar
offset = 1
let color = UIColor.init(red: 1, green: 1, blue: 1, alpha: offset)
self.navigationController?.navigationBar.barStyle = .default
self.navigationController?.navigationBar.tintColor = UIColor(hue: 1, saturation: offset, brightness: 1, alpha: 1)
self.navigationController?.navigationBar.backgroundColor = color
navigationController?.setStatusBar(backgroundColor: color)
} else { // hide navigation bar
let color = UIColor.init(red: 1, green: 1, blue: 1, alpha: offset)
self.navigationController?.navigationBar.tintColor = UIColor(hue: 1, saturation: offset, brightness: 1, alpha: 1)
self.navigationController?.navigationBar.backgroundColor = color
navigationController?.setStatusBar(backgroundColor: color)
self.navigationController?.navigationBar.barStyle = .black
}
}
The navigation bar works perfectly. However, the status bar doesn't change back to the transparent color when I scroll back up. [Screenshots below]
Initial State
Scroll A little bit
Scroll Back to initial State
I used this piece of code to update the status bar color:
extension UINavigationController {
func setStatusBar(backgroundColor: UIColor) {
let statusBarFrame: CGRect
if #available(iOS 13.0, *) {
statusBarFrame = view.window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero
} else {
statusBarFrame = UIApplication.shared.statusBarFrame
}
let statusBarView = UIView(frame: statusBarFrame)
statusBarView.backgroundColor = backgroundColor
view.addSubview(statusBarView)
}
}

Navigation Controller. Background color

For one controller I have settings:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
and
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
var offset = scrollView.contentOffset.y / 150
if offset > 1 {
offset = 1
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
self.navigationItem.title = name
} else {
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
self.navigationItem.title = ""
}
}
The main problem is that when i press back button, settings save. In the end i have white NavigationController. How can I make the settings not taken from the last controller?
func makeSearchController() {
searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.tintColor = .white
searchController.searchBar.placeholder = "Блюдо или продукт ..."
}
You can reset the navigation controller's color in viewWillDisappear, like this:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 221/255, green: 221/255, blue: 225/255, alpha: offset) //gray color
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 221/255, green: 221/255, blue: 225/255, alpha: offset) //gray color
}

How to add Animation Button like this?

How can I create an animated and shadowed button like in this video
(Once you click the button, it shines and swings)
Here is my code:
(This code implement swing and shadow but needs to be organized and arranged so that it is capable of running the light and the swing with one click and the button doesn't move from its place unnecessarily)
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var aBtn: ButtonWithShadow!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func btnClick(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
sender.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.50).cgColor
sender.layer.shadowOffset = CGSize(width: 0, height: 3)
sender.layer.shadowOpacity = 1.0
sender.layer.shadowRadius = 10.0
sender.layer.masksToBounds = false
} else {
sender.isSelected = true
sender.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.0).cgColor
}
aBtn.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
aBtn.layer.transform = CATransform3DMakeRotation(-.pi / 15, 0, 0, 1)
let needleAnim = CABasicAnimation(keyPath: "transform.rotation.z")
needleAnim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
needleAnim.duration = 1.0 as? CFTimeInterval ?? CFTimeInterval()
needleAnim.repeatCount = 5
needleAnim.autoreverses = true
// Setting fillMode means that, once the animation finishes, the needle stays in its end position.
needleAnim.fillMode = kCAFillModeForwards
needleAnim.isRemovedOnCompletion = true
needleAnim.toValue = Double.pi / 15
aBtn.layer.add(needleAnim, forKey: nil)
}
}
and
import UIKit
class ButtonWithShadow: UIButton {
override func draw(_ rect: CGRect) {
//updateLayerProperties()
}
}
Thanks for your time
Your code works as written and provides a swinging button when clicked. Make sure you control-click dragged from the button to the button definition.
You'll need to add two images to your button, one for the off state and one for the on state.
and on
Here is the intermediate result of adding setting the images.
and the way to change the images is to add them to the assets catalog and then change them with :
if sender.isSelected {
sender.isSelected = false
sender.setImage(#imageLiteral(resourceName: "off"), for: .normal)
sender.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.50).cgColor
sender.layer.shadowOffset = CGSize(width: 0, height: 3)
sender.layer.shadowOpacity = 1.0
sender.layer.shadowRadius = 10.0
sender.layer.masksToBounds = false
} else {
sender.isSelected = true
sender.setImage(#imageLiteral(resourceName: "onbtn"), for: .normal)
sender.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.0).cgColor
}

How to customize tintColor and resize UITabBarItem

The issues that I would like to change "Tintcolor" for each tabs. But the below code doesn't work at all.
And I added the button image and want to resize it using "UIEdgeInsetsMake". But the button is resized weirdly whenever I touched the button. I don't know why.
And I am using Swift 3.
class MainView: UITabBarController {
var TabFirst = UITabBarItem()
var TabSecond = UITabBarItem()
var TabThird = UITabBarItem()
var TabForth = UITabBarItem()
var TabFifth = UITabBarItem()
override func viewDidLoad() {
super.viewDidLoad()
tabBar.barTintColor = UIColor.white
TabFirst = self.tabBar.items![0]
TabFirst.image = UIImage(named: "btn_1-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
TabFirst.imageInsets = UIEdgeInsetsMake(12, 10, 11, 11)
tabBar.items?[0].title = "length"
TabSecond = self.tabBar.items![1]
TabSecond.image = UIImage(named: "btn_2-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBar.items?[1].title = "length"
TabThird = self.tabBar.items![2]
TabThird.image = UIImage(named: "btn_3-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBar.items?[2].title = "length"
TabForth = self.tabBar.items![3]
TabForth.image = UIImage(named: "btn_4-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBar.items?[3].title = "length"
TabFifth = self.tabBar.items![4]
TabFifth.image = UIImage(named: "btn_5-1")!//.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
tabBar.items?[4].title = "length"
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag{
case 0:
print("FirstTab")
UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 67/255.0, blue: 99/255.0, alpha: 1.0)
case 1:
print("SecondTab")
UITabBar.appearance().tintColor = UIColor(red: 237/255.0, green: 193/255.0, blue: 53/255.0, alpha: 1.0)
case 2:
print("ThirdTab")
UITabBar.appearance().tintColor = UIColor(red: 70/255.0, green: 183/255.0, blue: 128/255.0, alpha: 1.0)
case 3:
print("ForthTab")
UITabBar.appearance().tintColor = UIColor(red: 12/255.0, green: 195/255.0, blue: 199/255.0, alpha: 1.0)
case 4:
print("FifthTab")
UITabBar.appearance().tintColor = UIColor(red: 105/255.0, green: 72/255.0, blue: 170/255.0, alpha: 1.0)
default:
break
}
}
override func viewWillAppear(_ animated: Bool) {
UIApplication.shared.isStatusBarHidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
EDIT:
You are missing breaks in your switch statement:
switch item.tag{
Also, you are doing a switch on the tag and I don't see anywhere you have tagged them accordingly in your code. You should get the index of the item instead.
I am not a Swift coder, this is how you do it in Objective-C to give you a hint:
NSInteger indexOfTab = [[self.tabBar items] indexOfObject:item];
Then you do your switch statement of indexOfTab.
Here is the Swift version.:
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the selected index is : \(tabBar.items.index(of: item))")
}
If you want to individually change the "tintColor" , you should set a custom selectedImage instead.
Beware:
By default, unselected and selected images are automatically created
from the alpha values in the source images. To prevent system
coloring, provide images with alwaysOriginal.
As far as the documentation goes, there are no "tintColor" property for a UITabBarItem.
However, the UITabBar itself has a tintColor property. But this is not setting anything individually.
Tint Color
You can specify a custom tint color for the bar background using the
Tint (barTintColor) field. The default background tint color is white.
Use the Image Tint (selectedImageTintColor) field to specify the bar
item’s tint color when that tab is selected. By default, that color is
blue.
Regarding your resize methods, you should resize your original image instead, or check this question if it does fit your needs. However, the UITabBar and UITabBarItem customizations are limited to what you can read in the documentations.
If you want to further customize things individually, I suggest you search for or create a custom solution instead.