Swift constraint error when adding UINavigationBar - swift

I am trying to add a custom navigation bar to my app, but whenever it loads I get the 'Unable to simultaneously satisfy constraints' error. I can't seem to programatically remove any of the constraints - and I definitely haven't added any.
Console Output:
"<NSLayoutConstraint:0x600000092750 UILabel:0x7ffc40507d20'Configure'.firstBaseline == UILayoutGuide:0x6000007b18e0'TitleView(0x7ffc40503120)'.top + 23 (active)>",
"<NSLayoutConstraint:0x600000092840 UILabel:0x7ffc40507d20'Configure'.top >= UILayoutGuide:0x6000007b18e0'TitleView(0x7ffc40503120)'.top (active)>"
App Delegate:
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController(navigationBarClass: CustomNavBar.self, toolbarClass: nil)
let mainView = TableViewController()
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window!.makeKeyAndVisible()
CustomNavBar Class:
self.frame.size.height = 60
self.backgroundColor = appColour
self.tintColor = UIColor.white
self.titleTextAttributes = [
NSForegroundColorAttributeName : UIColor.white,
NSFontAttributeName : UIFont(name: "Avenir-Heavy", size: 30)!
]
self.setTitleVerticalPositionAdjustment(-5, for: .default)
ViewController:
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(start))
navigationItem.rightBarButtonItem!.imageInsets = UIEdgeInsets(top: -7, left: 0, bottom: 0, right: 0)
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "Folder")!, style: .plain, target: self, action: #selector(viewSaved))
navigationItem.leftBarButtonItem!.imageInsets = UIEdgeInsets(top: -7, left: 0, bottom: 0, right: 0)
self.title = "Configure"
I just can't figure out what to do.
Thanks in advance!

The issue is with your font-size in your CustomNavBar Class, reduce the font size until you don't get the constraint error.
self.titleTextAttributes = [
NSForegroundColorAttributeName : UIColor.white,
NSFontAttributeName : UIFont(name: "Avenir-Heavy", size: <30>)!
]

Related

UItoolbar as UITextField inputAccessoryView barButton items out of the safe area in landscape mode

adding a UIToolBar to an UItextField.inputAccessoryView in iOS 16 in landscape mode make the buttons on the right and left be outside the safe area adjusting to superViewEdges instead to safeArealayout.
let doneToolBar = UIToolbar()
let doneBtn = UIBarButtonItem(title: "Done",
style: .plain,
target: self,
action: #selector(self.dismissMyKeyboard))
let upBttn = UIBarButtonItem(image: UIImage(systemName: "chevron.up"),
style: .plain,
target: self,
action: #selector(upAction))
let downBttn = UIBarButtonItem(image: UIImage(systemName: "chevron.down"),
style: .plain,
target: self, action: #selector(downAction))
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let label = UILabel(frame: CGRect(x: 0,
y: 0,
width: self.view.frame.size.width / 3,
height: self.view.frame.size.height))
label.font = UIFont(name: "Helvetica", size: 13)
label.backgroundColor = UIColor.clear
label.textColor = UIColor.hexStringToUIColor(hex: viewModel.action.primaryColor)
label.text = "Select an expiration date"
label.textAlignment = NSTextAlignment.center
let textBtn = UIBarButtonItem(customView: label)
doneToolBar.items = [upBttn, downBttn, flexSpace, textBtn, flexSpace, doneBtn, flexSpace]
doneToolBar.sizeToFit()
doneToolBar.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin ]
doneToolBar.tintColor = UIColor.hexStringToUIColor(hex: viewModel.action.primaryColor)
cardExpTextField.inputAccessoryView = doneToolBar
the code to set the toolbar. and this is the result
tried in iPhone X with iOS 16.1 and simulator. but in simulator with iOS 15.5 work perfect
after rotate to portrait and going back to landscape the button fix the safe are perfect

UILabel Not Showing Up On View

I just started learning how to code without using storyboards programatically, so please treat me as a beginner... Even though my image shows up on the screen, the label doesn't... I am a little confused as to why this is going on and I appreciate your help. Again, I'm a beginner, do don't mind if its a silly mistake!!!
import UIKit
class LocationRequestController : UIViewController
{
let imageView: UIImageView =
{
let iconImageView = UIImageView()
iconImageView.contentMode = .scaleAspectFit
iconImageView.image = UIImage(named: "blue-pin")
return iconImageView
}()
let allowLocationLabel : UILabel =
{
let label = UILabel()
let attributedText = NSMutableAttributedString(string: "Allow Location\n", attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 24)])
attributedText.append(NSAttributedString(string: "Please enable location services For The Map To Work!", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]))
label.numberOfLines = 0
label.textAlignment = .center
label.attributedText = attributedText
return label
}()
override func viewDidLoad()
{
super.viewDidLoad()
configureViewAppearance()
}
func configureViewAppearance()
{
view.backgroundColor = .white
view.addSubview(imageView)
imageView.anchor(top: view.topAnchor, left: nil, bottom: nil, right: nil, paddingTop: 140, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 200, height: 200)
imageView.centerX(inView: view)
view.addSubview(allowLocationLabel)
allowLocationLabel.anchor(top: imageView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 32, paddingLeft: 32, paddingBottom: 0, paddingRight: 32, width: 0, height: 0)
allowLocationLabel.centerX(inView: view)
}
}
Thanks again!
You just need to add one line label.translatesAutoresizingMaskIntoConstraints
let allowLocationLabel : UILabel =
{
let label = UILabel()
let attributedText = NSMutableAttributedString(string: "Allow Location\n", attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 24)])
attributedText.append(NSAttributedString(string: "Please enable location services For The Map To Work!", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]))
label.numberOfLines = 0
label.textAlignment = .center
label.attributedText = attributedText
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
This was the problem:
The text color was automatically set to .white, so that's the reason why it didn't seem to show up! Thanks for your help everyone!

UIToolbar doesn't show again in view when dismissing keyboard in Swift

I have an UIToolbar containing a UITextField. I want to show the toolbar above the keyboard and then display it again when I finish editing inside the textfield.
The issue is that it is working when selecting the textfield. However, the toolbar is disappearing from the view when hiding the keyboard.
What should be done to redisplay it again to my view?
My Swift code is:
Add UITextFieldDelegate
class ATCChatThreadViewController: MessagesViewController, MessagesDataSource, MessageInputBarDelegate, UITextFieldDelegate {
Create ToolBar is as below
func createToolbar(){
//Fixed space
let fixed = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
fixed.width = 10
//Camera
//let img = UIImage(named: "camera-filled-icon")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
let img = UIImage.localImage("camera-filled-icon", template: true)
let iconSize = CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30))
let iconButton = UIButton(frame: iconSize)
iconButton.setTitleColor(uiConfig.primaryColor, for: .normal)
iconButton.setBackgroundImage(img, for: .normal)
let cameraItem = UIBarButtonItem(customView: iconButton)
cameraItem.tintColor = uiConfig.primaryColor
iconButton.addTarget(self, action: #selector(cameraButtonPressed), for: .touchUpInside)
//TextField true
textFieldChat = UITextField(frame: CGRectMake(0,0,(self.view.frame.size.width - 100) ,30))
textFieldChat.tintColor = uiConfig.primaryColor
textFieldChat.textColor = uiConfig.inputTextViewTextColor
textFieldChat.backgroundColor = uiConfig.inputTextViewBgColor
textFieldChat.layer.cornerRadius = 14.0
textFieldChat.layer.borderWidth = 0.0
textFieldChat.font = UIFont.systemFont(ofSize: 16.0)
textFieldChat.delegate = self
textFieldChat.attributedPlaceholder = NSAttributedString(string: "Start typing...".localized(), attributes: [NSAttributedString.Key.foregroundColor: uiConfig.inputPlaceholderTextColor])
let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20))
textFieldChat.leftView = paddingView
textFieldChat.leftViewMode = .always
let textFieldButton = UIBarButtonItem(customView: textFieldChat)
//Fixed space
let fixedTwo = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
fixedTwo.width = 10
//Send Button
let imgSend = UIImage.localImage("share-icon", template: true)
let iconSendSize = CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30))
let sendButton = UIButton(frame: iconSendSize)
sendButton.setTitleColor(uiConfig.primaryColor, for: .normal)
sendButton.setBackgroundImage(imgSend, for: .normal)
let sendItem = UIBarButtonItem(customView: sendButton)
sendItem
.tintColor = uiConfig.primaryColor
sendButton.addTarget(self, action: #selector(sendBtnPressedWith), for: .touchUpInside)
//Flexible Space
// let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)
let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
flexible.width = 10
//Toolbar
var bottomsafeAreaHeight: CGFloat?
if #available(iOS 11.0, *) {
bottomsafeAreaHeight = UIApplication.shared.windows.first{$0.isKeyWindow }?.safeAreaInsets.bottom ?? 0
} else {
// Fallback on earlier versions
bottomsafeAreaHeight = bottomLayoutGuide.length
bottomsafeAreaHeight = UIApplication.shared.keyWindow?.rootViewController?.bottomLayoutGuide.length ?? 0
}
toolbar = UIToolbar(frame: CGRectMake(0,(self.view.frame.size.height - 116 - (bottomsafeAreaHeight ?? 0)),view.frame.width,50))
toolbar.sizeToFit()
toolbar.barTintColor = UIColor.f5f5f5ColorBg()
toolbar.isTranslucent = false
toolbar.tintColor = uiConfig.primaryColor
toolbar.items = [fixed, cameraItem, fixed, textFieldButton,fixedTwo, sendItem,flexible]
view.addSubview(toolbar)
}
// MARK: - UITextFieldDelegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textFieldChat.inputAccessoryView = toolbar
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
view.addSubview(toolbar)
self.toolbar.isHidden = false
return true
}
if only chat etxfield is using toolbar then replace line:
view.addSubview(toolbar)
with
textFieldChat.inputAccessoryView = toolbar
remove both methods textFieldShouldBeginEditing and textFieldShouldEndEditing

Swift UIBarButtonItem position that was created programatically

I create a button using SwiftIcons, but the position of the icon is not correct. I add a negative width to fix it:
let menuButton = UIBarButtonItem()
menuButton.setIcon(icon: .ionicons(.chevronLeft), iconSize: 24, color: .white, cgRect: CGRect(x: 0, y: 0, width: 24, height: 24), target: self, action: #selector(menuButtonClick))
let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
negativeSpacer.width = -13.7;
self.navigationItem.leftBarButtonItems = [negativeSpacer, menuButton]
This works for iOS 10 or lower, but on iOS 11 the width does not take negative values.
Image
How can I fix this?
Answer:
let menuButton = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(menuButtonClick))
menuButton.image = UIImage.init(icon: .ionicons(.chevronLeft), size: CGSize(width: 24, height: 24))
menuButton.imageInsets = UIEdgeInsetsMake(0, -13.7, 0, 0)
self.navigationItem.leftBarButtonItem = menuButton
Maybe you can try by modifying edgeInsets for your button with this method :
UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);
It allows you to add some padding on your barButtonItem.
Here is how you can use it with your actual code :
negativeSpacer.imageInsets = UIEdgeInsetsMake(0, -13.7, 0, 0);

How can I add UIToolbar to UIPickerView without using UITextField?

I have used UIPickerView without UITextField. After user tap on button, UIPickerView will display.
Anyway, I have no idea how to dismiss UIPickerView. I have tried to set inputAccessoryView with UIToolbar but the complier said it's get-only property.
My code:
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
toolbar.isTranslucent = true
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(RegisterViewController.pickerDoneButton))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(RegisterViewController.pickerCancelButton))
toolbar.setItems([cancelButton, spaceButton, doneButton], animated: true)
toolbar.isUserInteractionEnabled = true
pickerView.inputAccessoryView = toolbar
Error: Cannot assign to property: 'inputAccessoryView' is a get-only property
Try to add subview to the UIPickerView
It seems like UIPickerView is not letting its children receive touch events. If you want to add toolbar to pickerview without textfield you can create a top level container UIView to hold both toolbar and picker view like this:
let picker = UIView(frame: CGRect(x: 0, y: view.frame.height - 260, width: view.frame.width, height: 260))
// Toolbar
let btnDone = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.monthdoneButtonAction))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(self.cancelClick))
let barAccessory = UIToolbar(frame: CGRect(x: 0, y: 0, width: picker.frame.width, height: 44))
barAccessory.barStyle = .default
barAccessory.isTranslucent = false
barAccessory.items = [cancelButton, spaceButton, btnDone]
picker.addSubview(barAccessory)
// Month UIPIckerView
monthPicker = UIPickerView(frame: CGRect(x: 0, y: barAccessory.frame.height, width: view.frame.width, height: picker.frame.height-barAccessory.frame.height))
monthPicker.delegate = self
monthPicker.dataSource = self
monthpickerData = ["January","February","March","April","May","June","July","August","September","October","November","December"]
monthPicker.backgroundColor = UIColor.white
picker.addSubview(monthPicker)