CNContactViewController button action - ios

I am using CNContactViewController. But, I want to add a new button. How to I do
If you can not add a new button then how can I get the action of the Call button?

can you try like this but i have used for new contact and you can customize with you code.
func CreateNewContact() {
let contact = CNMutableContact()
let unkvc = CNContactViewController(forNewContact: contact)
unkvc.delegate = self
unkvc.allowsEditing = true
unkvc.allowsActions = true
unkvc.title = "Create New Contact"
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 234.0/255.0, green: 34.0/255.0, blue: 39.0/255.0, alpha: 1.0)
self.navigationController?.navigationBar.tintColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
Application.Delegate.setStatusBarBackgroundColor(color: UIColor(red: 198/255.0, green: 6/255.0, blue: 39.0/255.0, alpha: 1.0))
self.navigationController?.navigationItem.hidesBackButton = true
let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(ShowInfotoshare.doneButtonClicked(_:)))
self.navigationController?.navigationItem.rightBarButtonItem = rightButton
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
self.navigationController?.pushViewController(unkvc, animated: false)
}
func doneButtonClicked(_ button:UIBarButtonItem!){
print("Done clicked")
}
If you have any query then show me your code so i will help you.

AFAIK, the row of buttons under the name (lets call them "methods of communications") cannot be modified. You can't hide the row, or individual buttons, you can't add anything into the row.
Also, the buttons active/inactive state is controlled by the current contact information (that is displayed below it).
Finally, note that the simulator, because it doesn't present a phone calling service nor include Mail.app, the buttons status will be pretty different from a real device.

Related

UINavigationBar tintColor is misbehaving

I have issue when set UINavigationBar tintColor.
It is misbehaving
I'm using xcode 11.3.1, swift 5, iOS 13.3
*MyClass
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "test"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
}
*result
real device
https://imgur.com/sONr4vq
simulator
https://imgur.com/vs5lhgR
I want to set back button color like title
I only get error on real device
Please help me
Thank you
To set back button and hide "back":
let backItem = UIBarButtonItem()
backItem.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
navigationItem.backBarButtonItem = backItem
You should but this code to the ViewController before push the new one.
Try access to navigationController.navigationBar instead of navigationController.view :
self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
Did you try using UIColor instead of #colorLiteral?
Like
self.navigationController?.navigationBar.tintColor = UIColor(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
//or
self.navigationController?.navigationBar.tintColor = UIColor(displayP3Red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
Edit:
Since that the above did not work for you, did you try this?
self.navigationController?.navigationBar.tintColor = self.navigationController?.navigationItem.titleView?.backgroundColor
Considering that all you want is to have both the button and title to have the same colour right?

Adding UIButton to toolbar swift 5

so I have read other stack overflow ages and none seem to help me. I have a piece of code that creates my UIButton which is here:
let haveAccountButton: UIButton = {
let HColor = UIColor(red: 89/255, green: 156/255, blue: 120/255, alpha: 1)
let HFont = UIFont.systemFont(ofSize: 16)
let HSColor = UIColor(red: 239/255, green: 47/255, blue: 102/255, alpha: 1)
let HButton = UIButton(type: .system)
let attributedTitle = NSMutableAttributedString(string:
NSLocalizedString("Already have an account?", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: HFont])
attributedTitle.append(NSAttributedString(string: NSLocalizedString(" Sign In", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: HSColor, NSAttributedString.Key.font: HFont]))
HButton.addTarget(self, action: #selector(signinAction), for: .touchUpInside)
HButton.setAttributedTitle(attributedTitle, for: .normal)
return HButton
}()
I already set navigationController?.isToolbarHidden = false in viewDidLoad. my question is how do I make the button appear in the toolbar?
UPDATE: The way this code will run currently without the toolbar is as follows:
import Foundation
import UIKit
class SignUpControllerSave: UIViewController {
let haveAccountButton: UIButton = {
let HColor = UIColor(red: 89/255, green: 156/255, blue: 120/255, alpha: 1)
let HFont = UIFont.systemFont(ofSize: 16)
let HSColor = UIColor(red: 239/255, green: 47/255, blue: 102/255, alpha: 1)
let HButton = UIButton(type: .system)
let attributedTitle = NSMutableAttributedString(string:
NSLocalizedString("Already have an account?", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: HFont])
attributedTitle.append(NSAttributedString(string: NSLocalizedString(" Sign In", comment: ""), attributes: [NSAttributedString.Key.foregroundColor: HSColor, NSAttributedString.Key.font: HFont]))
HButton.addTarget(self, action: #selector(signinAction), for: .touchUpInside)
HButton.setAttributedTitle(attributedTitle, for: .normal)
return HButton
}()
//potential gradient background if wanted
func setGradientBackground() {
let top_Color = UIColor(red: 203/255, green: 215/255, blue: 242/255, alpha: 1.0).cgColor
let bottom_Color = UIColor(red: 181/255, green: 199/255, blue: 242/255, alpha: 1.0).cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [top_Color, bottom_Color]
gradientLayer.locations = [0, 1]
gradientLayer.frame = self.view.bounds
self.view.layer.insertSublayer(gradientLayer, at: 0)
}
override func viewDidLoad() {
super.viewDidLoad()
//view.backgroundColor = .yellow
navigationController?.isNavigationBarHidden = true
navigationController?.isToolbarHidden = false
navigationItem.title = NSLocalizedString("Membership", comment: "")
setupHaveAccountButton()
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.isNavigationBarHidden = true
navigationController?.isToolbarHidden = false
setGradientBackground()
super.viewWillAppear(animated)
}
#objc func signinAction() {
navigationController?.popViewController(animated: true)
}
fileprivate func setupHaveAccountButton() {
view.addSubview(haveAccountButton)
haveAccountButton.anchors(top: nil, topPad: 0, bottom: view.safeAreaLayoutGuide.bottomAnchor,
bottomPad: 8, left: view.leftAnchor, leftPad: 0, right: view.rightAnchor,
rightPad: 0, height: 20, width: 0)
}
}
UPDATE: The previous answer did allow for the button to now be on the toolbar, but the button target action does not work. I have tried adding .sizeToFit() where I can and looked at a lot of websites all to no avail. Anyone know how I can go about solving the button target issue?
This code should work the way you want:
class SignUpControllerSave: UIViewController {
/* the other code you posted can be used without changes */
fileprivate func setupHaveAccountButton() {
toolbarItems = [
UIBarButtonItem(customView: haveAccountButton)
]
}
}
As this seems to cause problems with regards to the click handler, try this (which looks almost the same – except for the font size):
class SignUpControllerSave: UIViewController {
fileprivate func setupHaveAccountButton() {
let haveAccountLabel = UILabel()
haveAccountLabel.font = UIFont.systemFont(ofSize: UIFont.buttonFontSize)
haveAccountLabel.text = NSLocalizedString("Already have an account?", comment: "")
haveAccountLabel.textColor = .lightGray
toolbarItems = [
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(customView: haveAccountLabel),
UIBarButtonItem(title: "Sign In", style: .plain, target: self, action: #selector(self.signinAction)),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
]
navigationController?.toolbar.tintColor = UIColor(red: 239/255, green: 47/255, blue: 102/255, alpha: 1)
}
}

How to commit changes of navigator bar translucency properly?

I have faced with such problem.
I have two VC. One of it has basic navigator bar, which inherits from main Navigator bar.
In this nav bar I use such settings to customise my nav nar
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationBar.createGradientNavigatorBar()
navigationBar.isTranslucent = false
navigationBar.barTintColor = UIColor.init(red: 0/255, green: 97/255, blue: 185/255, alpha: 1)
navigationBar.shadowImage = UIImage()
}
On the second VC I have to set custom gradient background which connected with view. For this purpose I have extended my view up to bar height and make par transculent. Here is code:
override func viewWillAppear(_ animated: Bool) {
isCurrentViewControllerAppropriate.shared.isCurrentVcAppropriate = isCurrentViewController()
let firstColor = UIColor.init(red: 0/255, green: 94/255, blue: 198/255, alpha: 1)
let secondColor = UIColor.init(red: 27/255, green: 145/255, blue: 226/255, alpha: 1)
profileBackgroundView.createGradient(firstColor: firstColor.cgColor, secondColor: secondColor.cgColor)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.backgroundColor = UIColor.clear
navigationController?.navigationBar.isTranslucent = true
}
override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.isTranslucent = false
}
Everything is working fine, but I have rough transition between vc. I've attached.
As you may notice when I return to my first VC is loading it have an artefact with navigator bar color from second VC, which resolves after a certain period of time. I have no idea how to deal with it
I'm not sure whether my approach is the right one, so I'll be glad to hear any advices consider solving this issue.

UIButton not working loaded in a child ViewController

I've got a MainViewController where I'm loading a slideout-menu. The MainViewController contains a static TopView and a ContentView where different child controllers or views are loaded, depending on which menu entry was selected.
There is a button inside one of the children is loaded, but it is not working.
When I start the app with the ViewController including the button, set to "is initial View Controller", everything works properly.
When starting the app with the MainViewController as initial View Controller with a loaded child, the button is not working (it is loaded in the view, but not reacting).
Any suggestions how I can make this work?
MainViewController loading of Child View
class MainViewController: UIViewController, SideBarDelegate {
var contentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
....
contentView.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
contentView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(contentView)
contentView.topAnchor.constraint(equalTo: statusBarView.bottomAnchor).isActive = true
contentView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
contentView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
contentView.layoutIfNeeded()
}
func sideBarDidSelectButtonAtIndex(_ index: Int) {
...
if index == 0{
statusBarLabel.text = "First"
let controller:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
controller.view.frame = ContentView.bounds
controller.willMove(toParentViewController: self)
contentView.addSubview(controller.view)
controller.didMove(toParentViewController: self)
print("touched index 0")
} else if index == 1{
// index is 1
}
}
FirstViewController
class FirstViewController: UIViewController, UIScrollViewDelegate {
let addButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
addButton.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
addButton.clipsToBounds = true
addButton.setTitleColor(UIColor.white, for: .normal)
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
addButton.setTitle("add feed", for: .normal)
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFontWeightRegular)
self.view.addSubview(AddButton)
addButton.layoutIfNeeded()
addButton.addTarget(self, action: #selector(touchUpAddButton), for: UIControlEvents.touchUpInside)
addButton.addTarget(self, action: #selector(touchDownAddButton), for: UIControlEvents.touchDown)
func touchUpAddButton() {
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
}
func touchDownAddButton() {
addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
}
}
While adding the view in the container you don't have to use addSubview function, that the FirstViewController functionality is not working.
Use below code:
let newViewController:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
let oldViewController = childViewControllers.last! as UIViewController
oldViewController.willMove(toParentViewController: nil)
addChildViewController(newViewController)
transition(from: oldViewController, to: newViewController, duration: 0.25, options: .transitionCrossDissolve, animations:{ () -> Void in
// nothing needed here
}, completion: { (finished) -> Void in
oldViewController.removeFromParentViewController()
newViewController.didMove(toParentViewController: self)
})
You can change the duration, it used for animation
Update your functions to these
func TouchUpAddButton(sender: UIButton){
addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
}
func TouchDownAddButton(sender: UIButton) {
addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
}
And change your code of adding the target to
addButton.addTarget(self, action: #selector(FirstViewController.touchUpAddButton(_:)), for: .touchDown)
addButton.addTarget(self, action: #selector(FirstViewController.touchDownAddButton(_:)), for: .touchDown)
First solution:
Remove an autolayout constrains
Second solution:
Button is not performing an action because a child view controller view should be added directly to a container controller view (in your case: MainViewController view) and not via an intermediate view (in your case: contentView)

How would I change background color of alert view

I am working on tvOS project, I need to create custom alert view with particular background color and text.
Finally , I have got the solution,
if let next = context.nextFocusedView as? UIButton {
next.setTitleColor(UIColor.blackColor(), forState: UIControlState.Focused)
next.backgroundColor = UIColor(red: 248/255, green: 175/255, blue: 2/255, alpha: 1.0)

Resources