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)
}
}
Related
I wanted to design tabs using segment control but the thing that i know regarding all its properties is just limited for now. the first image is my current one the 2nd image is what i want to achieve
What i really want to achieved is the segment control which has items all , pending , expired , finished. does anyone has an idea how to achieve distance between items in segments , also change the font color and style according to the design sample below. Thank You.
what i have so far.
let appearance = SMSegmentAppearance()
appearance.segmentOnSelectionColour = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
appearance.segmentOffSelectionColour = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
appearance.titleOnSelectionFont = UIFont.systemFont(ofSize: 25.0)
// segmentControlView.font = UIFont(name: "Avenir-Black", size: 12)
appearance.titleOffSelectionFont = UIFont.systemFont(ofSize: 25.0)
appearance.contentVerticalMargin = 10
/*
Init SMsegmentView
Set divider colour and width here if there is a need
*/
let segmentFrame = CGRect(x: self.margin, y: 5, width: self.segmentControlView.frame.size.width - self.margin*8, height: 50.0)
self.segmentView = SMSegmentView(frame: segmentFrame, dividerColour: UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0), dividerWidth: 1.0, segmentAppearance: appearance)
self.segmentView.backgroundColor = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
self.segmentView.layer.cornerRadius = 5.0
self.segmentView.layer.borderColor = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0).cgColor
self.segmentView.layer.borderWidth = 1.0
self.segmentView.addTarget(self, action: #selector(selectSegmentInSegmentView(segmentView:)), for: .valueChanged)
self.segmentView.addSegmentWithTitle("All", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Pending", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Finished", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Expired", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
// var ownerNames = [String]()
//
// if fetchedToDoItems.count > 0 {
// for item in fetchedToDoItems {
// if item.houseNo == loggedInUserHouseNumber &&
// !ownerNames.contains(item.ownerName!) {
// ownerNames.append(item.ownerName!)
// }
// }
//
// for name in ownerNames {
// // Add segments
// self.segmentView.addSegmentWithTitle(name, onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
// }
//
// }
// Set segment with index 0 as selected by default
self.segmentView.selectedSegmentIndex = 0
self.segmentControlView.addSubview(self.segmentView)
As i have Seen the documentation:
You just need to change the Font style and size:
You can try :
Step:1 Print fonts
func printFonts() {
let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName)
print("Font Names = [\(names)]")
}
}
Step :2 Set it to appearance:-
appearance.titleOnSelectionFont = UIFont(name: "TradeGothicLTStd-Cn18", size: 10.0)!
appearance.titleOffSelectionFont = UIFont(name: "TradeGothicLTStd-Cn18", size: 10.0)!
Hope this help!
Edit to set Selected to Black and Unselected to White
appearance.segmentOffSelectionColour = UIColor.white //UnSelected Colour
appearance.segmentOnSelectionColour = UIColor.black //Selected Colour
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.
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)
I want to go to another view controller when pressing button my btnSignUp, If I write code like this I have error "sigabrt". What I should do?
import UIKit
import SnapKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)
createTop()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func createTop() {
let topView = UIView()
self.view.addSubview(topView)
topView.backgroundColor = UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)
topView.snp_makeConstraints { (make) -> Void in
make.width.equalTo((self.view.frame.width/10)*8)
make.height.equalTo(self.view.frame.height/5)
make.centerX.equalTo(self.view)
make.top.equalTo(self.view).offset(self.view.frame.height/15)
}
let btnSignUp = UIButton()
self.view.addSubview(btnSignUp)
btnSignUp.backgroundColor = UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)
btnSignUp.layer.borderColor = UIColor.whiteColor().CGColor
btnSignUp.layer.borderWidth = 1
btnSignUp.layer.cornerRadius = 6
btnSignUp.setTitle("Sign Up", forState: UIControlState.Normal)
btnSignUp.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 17)
btnSignUp.snp_makeConstraints { (make) -> Void in
make.width.equalTo(((self.view.frame.width/10)*7)+40)
make.height.equalTo(self.view.frame.height/13)
make.top.equalTo(ORView.snp_bottom).offset(20)
make.centerX.equalTo(self.view)
}
btnSignUp.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
func buttonAction(sender:UIButton!)
{
let signUpVC = SignUpViewController()
self.presentViewController(signUpVC, animated: true, completion: nil)
}
}
}
Your action selector buttonAction doesn't match your function declaration - It takes an argument, so the selector will be buttonAction:
btnSignUp.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
Additionally, check your parentheses. It appears that your buttonAction function is declared inside func createTop()
func createTop() {
let topView = UIView()
self.view.addSubview(topView)
topView.backgroundColor = UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)
topView.snp_makeConstraints { (make) -> Void in
make.width.equalTo((self.view.frame.width/10)*8)
make.height.equalTo(self.view.frame.height/5)
make.centerX.equalTo(self.view)
make.top.equalTo(self.view).offset(self.view.frame.height/15)
}
let btnSignUp = UIButton()
self.view.addSubview(btnSignUp)
btnSignUp.backgroundColor = UIColor(red: 15/255, green: 52/255, blue: 100/255, alpha: 1)
btnSignUp.layer.borderColor = UIColor.whiteColor().CGColor
btnSignUp.layer.borderWidth = 1
btnSignUp.layer.cornerRadius = 6
btnSignUp.setTitle("Sign Up", forState: UIControlState.Normal)
btnSignUp.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 17)
btnSignUp.snp_makeConstraints { (make) -> Void in
make.width.equalTo(((self.view.frame.width/10)*7)+40)
make.height.equalTo(self.view.frame.height/13)
make.top.equalTo(ORView.snp_bottom).offset(20)
make.centerX.equalTo(self.view)
}
btnSignUp.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
}
func buttonAction(sender:UIButton!) {
let signUpVC = SignUpViewController()
self.presentViewController(signUpVC, animated: true, completion: nil)
}
Finally, I doubt that you want to get your new view controller by SignUpViewController() - if you are using a storyboard you want to call instantiateViewControllerWithIdentifier
I am trying to add button to the programatically created view that I add to main view. I can't make action to work on a button. This is what I tried so far:
itemView = UIImageView(frame:CGRect(x:0, y:0, width:240, height:375))
itemView.backgroundColor = UIColor(red: 255.0/0.0, green: 255.0/0.0, blue: 255.0/0.0, alpha: 0.05)
itemView.contentMode = .Center
buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40))
buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0)
buttonConnect.setTitle("Connect", forState: .Normal)
buttonConnect.tag = 3
buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown)
itemView.addSubview(buttonConnect)
self.addSubview(itemView)
Method on button click looks like this:
func btnConnectTouched(sender:UIButton!)
{
print("button connect touched")
}
Can someone advice what is the problem? Thanks!
I don't understand why you are adding a button inside a UIImageView. Even this is what you need, take a UIView and then put all other controls inside it. The reason your button is not clicking is that you have put it inside a UIImageView and for UIImageView , default value for userInteractionEnabled is false. so you need to enable it i.e.
itemView.userInteractionEnabled = true;
Also set button target to 'self' rather than itemView.
Try change the tarjet and the event
buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown)
into
buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:. TouchUpInside)
You are wrong at the target:
// buttonConnect.addTarget(itemView, action: "btnConnectTouched:", forControlEvents:.TouchDown)
// using
buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchDown)
// or buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchUpInside)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let buttonConnect = UIButton()
buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40))
buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0)
buttonConnect.setTitle("Connect", forState: .Normal)
buttonConnect.tag = 3
buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents: .TouchUpInside)
self.view.addSubview(buttonConnect)
}
func btnConnectTouched(sender: UIButton!) {
print("button connect touched");
}
try buttonConnect.addTarget(itemView, action: "btnConnectTouched", forControlEvents:.TouchUpInside) without the : after "btnConnectTouched"
If you can make following two changes in your code, it will work.
Replace UIImageView with UIView for itemView type and
Replace self.addSubview(itemView) with self.view.addSubview(itemView)
Your final code should look like....
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var itemView = UIView(frame:CGRect(x:0, y:0, width:240, height:375))
itemView.backgroundColor = UIColor(red: 255.0/0.0, green: 255.0/0.0, blue: 255.0/0.0, alpha: 0.05)
itemView.contentMode = .Center
var buttonConnect = UIButton(frame: CGRect(x:0, y:335, width:120, height:40))
buttonConnect.backgroundColor = UIColor(red: 0.8, green: 0.6, blue: 0.2, alpha: 1.0)
buttonConnect.setTitle("Connect", forState: .Normal)
buttonConnect.tag = 3
buttonConnect.addTarget(self, action: "btnConnectTouched:", forControlEvents:.TouchDown)
itemView.addSubview(buttonConnect)
self.view.addSubview(itemView)
}
func btnConnectTouched(sender:UIButton!)
{
print("button connect touched")
}
Try this one.... It's working for me....