Adding custom buttons to UITabBarController (adding button in the center) - swift

i'm trying to make a custom tab bar with some images i made and i'm having some trouble. I'm trying to add a button to the tab bar and it seems like i can't do it. I want to do something like this:
Then adding some animations to that button. How can i go about adding that button? Do i need to subclass UITabBarController? Thank you!

In TabbarController class, add this code
override func viewDidLoad() {
super.viewDidLoad()
setupMiddleButton()
}
// MARK: - AddButton
func setupCenterButton() {
let centerButton = UIButton(frame: CGRect(x: 0, y: 10, width: 45, height: 45))
var centerButtonFrame = centerButton.frame
centerButtonFrame.origin.y = (view.bounds.height - centerButtonFrame.height) - 2
centerButtonFrame.origin.x = view.bounds.width/2 - centerButtonFrame.size.width/2
centerButton.frame = centerButtonFrame
centerButton.layer.cornerRadius = 35
view.addSubview(centerButton)
centerButton.setBackgroundImage(#imageLiteral(resourceName: "tabPost"), for: .normal)
centerButton.addTarget(self, action: #selector(centerButtonAction(sender:)), for: .touchUpInside)
view.layoutIfNeeded()
}
// MARK: - Center button Actions
#objc private func centerButtonAction(sender: UIButton) {
selectedIndex = 2
}
It will work.. :)

You should implement the delegate method of UITabBarControllerDelegate:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController == (self.tabBarController?.viewControllers?[theIndexOfTheButton])! {
// do my stuffs here
return false
}
return true
}
Don't forget to set self.tabBarController?.delegate = self

Related

Animation segue bug on the navigation bar with large title

My bug:
If navigate from a view controller with large titles enabled to a view controller with large titles disabled i see same bug. Height navigation bar changes not smoothy.
I want animation change height navBar during segue on another viewController like this
Common propertyes for navBar set up in BaseNavigationController
class BaseNavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
setNavBarTitlesPropertyes()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
private func setNavBarTitlesPropertyes() {
navigationBar.tintColor = .white
navigationBar.titleTextAttributes = [
.foregroundColor: UIColor.white
]
if #available(iOS 11.0, *) {
navigationBar.prefersLargeTitles = true
navigationBar.largeTitleTextAttributes = [
.foregroundColor: UIColor.white
]
}
}
And my setting navbar in the storyboard:
I found solution for this trouble. UINavigationBar
property translucent should be true, and also bottom and top constraint for tableView in UIViewController should be equal Superview.Top and Superview.Bottom accordingly.

All objects disappear in view controller (main storyboard) when I use custom #IBDesignable UITextfield class. (Xcode 10, Swift 4.2)

All objects disappear in view controller (main storyboard) when I use custom #IBDesignable UITextfield class. Main storyboard looks like this:
(Xcode 10, Swift 4.2)
As you see, it shows an error also. But Xcode shows 'Build Succeeded' when I run the app.
I use custom #IBDesignable UITextfield class like this below:
import UIKit
import AKMaskField
#IBDesignable
class MyTextFieldStyle: AKMaskField {
#IBInspectable var secureEntry : Bool = false
#IBInspectable var myBorderColor : UIColor = UIColor.white {
didSet {
self.layer.borderColor = myBorderColor.cgColor
}
}
#objc func secureButtonPressed (sender: UIButton) {
self.secureEntry = !self.secureEntry
self.isSecureTextEntry = self.secureEntry
}
override func awakeFromNib() {
super.awakeFromNib()
if self.secureEntry {
let btn = UIButton(type: .custom)
btn.setImage(#imageLiteral(resourceName: "eye-17-glyph-16"), for: .normal)
btn.addTarget(self, action: #selector(self.secureButtonPressed(sender:)), for: .touchUpInside)
btn.sizeToFit()
btn.frame = CGRect(x: self.frame.size.width-(btn.frame.size.width+5), y: (self.frame.size.height-btn.frame.size.height)/2, width: btn.frame.size.width, height: btn.frame.size.height)
self.addSubview(btn)
}
self.layer.borderColor = self.myBorderColor.cgColor
}
}
It becomes fine when I close and open Xcode. It looks like a bug. This was working fine.

How to create a raised center tab bar button

I've examined many GitHub and StackOverflow solutions to creating a raised center tab bar button and tried multiple times into creating one but I always end of right back to square one.
So below are the steps of what I've been doing, if someone can solve the problem it would be greatly appreciated.
Created a Tab Bar Controller to navigate to 5 different View Controllers
Created a Custom Class for the Tab Bar Controller called CustomTabBarController
Inside my CustomTabBarController File is
import UIKit
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setupMiddleButton()
}
func setupMiddleButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height:64))
var menuButtomFrame = menuButton.frame
menuButtomFrame.origin.y = view.bounds.height - menuButtomFrame.height
menuButtomFrame.origin.x = view.bounds.width/2 - menuButtomFrame.size.width/2
menuButton.frame = menuButtomFrame
view.addSubview(menuButton)
menuButton.setImage(UIImage(named: "MidPhoto"), for: .normal)
menuButton.addTarget(self, action: #selector(menuButtonAction(sender:)), for: .touchUpInside)
view.layoutIfNeeded()
tabBarController?.tabBar.addSubview(menuButton)
}
#objc private func menuButtonAction(sender: UIButton) {
selectedIndex = 2
}
}
That's it. I only edited 1 file (CustomTabBarController) and it works but the image is appearing into unwanted View Controllers and I've done multiple things like: Hide Bottom Bar On Push, self.tabBarController?.tabbar.isHidden = true, and so on.
What can I do to fix this?

Swift: connect a static button with a static method

Say I have a static button member variable, as well as a static method which handles its click. How can I connect the button with the method? I couldn't figure out how to get addTarget to work in this case:
private static let my_button: UIButton = {
let button = UIButton(type: .system)
...
button.addTarget(???, action: #selector(handleButtonClick), for: .touchUpInside)
return button
}()
private static func handleButtonClick() {
...
}
Could I put UIApplication.shared.keyWindow!.rootViewController in place of ????
Details
xCode 8.2.1, Swift 3
Full sample
import UIKit
class Button:NSObject {
class func createButton() -> UIButton {
let button = UIButton(type: .system)
button.frame = CGRect(x: 40, y: 40, width: 200, height: 40)
button.setTitle("Text", for: .normal)
button.addTarget(self, action: #selector(Button.handleButtonClick), for: .touchUpInside)
return button
}
class func handleButtonClick() {
print("Click")
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.addSubview(Button.createButton())
}
}
Result
You have to have an actual object - an instance of your class - as the button's target. It might be possible to pass the class object, but that's not how it's supposed to work. The button basically expects its target to be an object that inherits from NSResponder.

swift Button display

I create a button from my code.
How can I write code to let button change to like Default?
Am I missing some code?
I have tried adjustsImageWhenHighlighted or something ....
I mean when I push Button ..the button will become to transparent(I still push on) and when i push off will become back ...
like down below two images...
import UIKit
class ViewController: UIViewController {
var testButton:UIButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.whiteColor()
testButton.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
testButton.backgroundColor = UIColor.whiteColor()
testButton.setTitle("123", forState:.Normal)
testButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
testButton.tag = 1
testButton.addTarget(self, action: #selector(number), forControlEvents: .TouchUpInside)
self.view.addSubview(testButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func number(){
print("\("100")")
}
}
You could either add:
testButton.setTitleColor(UIColor.redColor(), forState: .Highlighted)
or have the colour applied in the button's function:
#IBAction func testButton(sender: UIButton) {
testButton.titleLabel.textColor = UIColor.redColor()
}
I've used redColor here for the examples, but you could change that to whatever colour you want to create the desired effect. I've not actually compiled this, so apologies if you need to correct any of the syntax to get it to work.