Weird width in UITableView on iOS 11 - ios

I just added an UITableView with top UISearchBar into my view but only on iOS 11, the table view has more width than my search bar, this is my code:
self.autocompleteTableView = UITableView(frame: CGRect(x: self.storeSearchBar.frame.origin.x, y: self.storeSearchBar.frame.origin.y + self.storeSearchBar.frame.size.height, width: self.storeSearchBar.bounds.width, height: 200.0))
self.autocompleteTableView!.alpha = 0.8
self.autocompleteTableView!.delegate = self
self.autocompleteTableView!.dataSource = self
self.autocompleteTableView!.separatorStyle = .none
if #available(iOS 11.0, *) {
self.autocompleteTableView!.insetsContentViewsToSafeArea = true
}
self.storeSearchBar.alpha = 0.8
self.storeSearchBar.delegate = self
self.view.addSubview(self.autocompleteTableView!)
self.autocompleteTableView!.isHidden = true
let searchBar = self.storeSearchBar!
searchBar.barTintColor = UIColor.white
searchBar.sizeToFit()
What am I doing wrong?

so, thanks to #MaulikBhuptani, the answer is:
override func viewDidLayoutSubviews() {
self.autocompleteTableView = UITableView(frame: CGRect(x:
self.storeSearchBar.frame.origin.x, y: self.storeSearchBar.frame.origin.y + self.storeSearchBar.frame.size.height, width: self.storeSearchBar.bounds.width, height: 200.0))
self.autocompleteTableView!.alpha = 0.8
self.autocompleteTableView!.delegate = self
self.autocompleteTableView!.dataSource = self
self.autocompleteTableView!.separatorStyle = .none
if #available(iOS 11.0, *) {
self.autocompleteTableView!.insetsContentViewsToSafeArea = true
}
self.storeSearchBar.alpha = 0.8
self.storeSearchBar.delegate = self
self.view.addSubview(self.autocompleteTableView!)
self.autocompleteTableView!.isHidden = true
let searchBar = self.storeSearchBar!
searchBar.barTintColor = UIColor.white
searchBar.sizeToFit()
}

Related

I cannot stretch the height of the titleView in the UINavigationBar

This is my code:
class UINavigationControllerCustom : UINavigationController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews();
navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 120);
}
}
class PreferenceInput: UIViewController {
override func viewDidLoad() {
let l1 = UILabel()
l1.backgroundColor = .clear
l1.numberOfLines = 1
l1.font = UIFont.boldSystemFont(ofSize: 30.0)
l1.textAlignment = .left
l1.textColor = .black
l1.text = "Bold title"
l1.sizeToFit();
let l2 = UILabel()
l2.backgroundColor = .clear
l2.numberOfLines = 2
l2.font = UIFont.boldSystemFont(ofSize: 16.0)
l2.textAlignment = .left
l2.textColor = .darkGray;
l2.text = "This is a\nmultiline string for the navBar"
l2.sizeToFit();
let tView = UIStackView(arrangedSubviews: [l1,l2]);
tView.axis = .vertical;
let spacer = UIView()
let constraint = spacer.widthAnchor.constraint(greaterThanOrEqualToConstant: CGFloat.greatestFiniteMagnitude)
constraint.isActive = true
constraint.priority = .defaultLow
let stack = UIStackView(arrangedSubviews: [tView, spacer])
stack.axis = .horizontal
navigationItem.titleView = stack
navigationItem.titleView.sizeToFit();
}
}
And this is what I get:
I can set the height of the navigation bar but how and where should I set the constraint to stretch the height in respect of the given 120px of the custom navigation controller?
I want to provide an individual height of the navigation bar.

Messagekit Navigation bar is not showing up

How can i show the default or custom navigation bar ? tried everything but nothing works , It seems like nothing is on the top , its a messageViewcontroller of messagekit and couldn't find any delegate method for navigation bar , it would be nice if someone educate me about this ..
My Code
override func viewDidLoad() {
messagesCollectionView = MessagesCollectionView(frame: .zero, collectionViewLayout: CustomMessagesFlowLayout())
messagesCollectionView.register(CustomCell.self)
super.viewDidLoad()
messagesCollectionView.messagesDataSource = self
messagesCollectionView.messagesLayoutDelegate = self
messagesCollectionView.messagesDisplayDelegate = self
messagesCollectionView.messageCellDelegate = self
messageInputBar.delegate = self
configureMessageInputBar()
configureInputBarItems()
updateTitleView(title: "Hanzala", subtitle: "Online")
}
import UIKit
extension UIViewController {
func updateTitleView(title: String, subtitle: String?, baseColor: UIColor = .white) {
let titleLabel = UILabel(frame: CGRect(x: 0, y: -2, width: 0, height: 0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = baseColor
titleLabel.font = UIFont.systemFont(ofSize: 15)
titleLabel.text = title
titleLabel.textAlignment = .center
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.sizeToFit()
let subtitleLabel = UILabel(frame: CGRect(x: 0, y: 18, width: 0, height: 0))
subtitleLabel.textColor = baseColor.withAlphaComponent(0.95)
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.textAlignment = .center
subtitleLabel.adjustsFontSizeToFitWidth = true
subtitleLabel.sizeToFit()
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: max(titleLabel.frame.size.width, subtitleLabel.frame.size.width), height: 30))
titleView.addSubview(titleLabel)
if subtitle != nil {
titleView.addSubview(subtitleLabel)
} else {
titleLabel.frame = titleView.frame
}
let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
if widthDiff < 0 {
let newX = widthDiff / 2
subtitleLabel.frame.origin.x = abs(newX)
} else {
let newX = widthDiff / 2
titleLabel.frame.origin.x = newX
}
navigationItem.titleView = titleView
}
}
I want the navigation bar like this
Pushing ChatViewController in NavigationController
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let model = Api.Params.chatUser[indexPath.row]
openConversation(model)
}
func openConversation(_ model: ChatUser) {
Api.Params.inputRecieverId = model.userId
let id = String(requestManager.instance.userID)
let vc = ChatViewController(recieverId: model.userId, senderId: id, conversationId: "Eman-Conversation-\(id)-\(model.userId)")
vc.title = model.username
vc.navigationItem.largeTitleDisplayMode = .never
navigationController?.pushViewController(vc, animated: true)
}
You've to embed the MessageViewcontroller inside a UINavigationController and use it.
let navigationController = UINavigationController(rootViewController: MessageViewcontroller())
If the controller that you're pushing MessageViewcontroller onto already has a navigationController then push the MessageViewcontroller into the navigational stack instead of presenting.
let messageViewcontroller = MessageViewcontroller()
navigationController?.pushViewController(messageViewcontroller, animated: true)

How to remove navigation bar from view controller

SearchViewController viewDidLoad has the following functionality
override func viewDidLoad() {
super.viewDidLoad()
self.cache = NSCache()
let path = "books";
self.ref = Database.database().reference().child(path);
tableViewBooks.dataSource = self
tableViewBooks.delegate = self
tableViewBooks.register(BookDetailTableViewCell.self, forCellReuseIdentifier: "book")
// Uncomment the following line to preserve selection between presentations
self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchBar.searchBarStyle = UISearchBar.Style.prominent
searchController.searchBar.isTranslucent = false
let searchBarView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(45)))
let view = UIView();
view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width , height: 54)
let searchBar = searchController.searchBar
searchBar.backgroundColor = UIColor.white
searchBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width , height: 44)
searchBar.searchBarStyle = UISearchBar.Style.prominent
searchBar.placeholder = " Search book title..."
searchBar.sizeToFit()
view.backgroundColor = UIColor.white
searchBar.isTranslucent = false
searchBarView.addSubview(searchBar)
view.addSubview(searchBarView)
tableView.tableHeaderView = view
definesPresentationContext = true
tableView.separatorStyle = .none
}
When someone clicks on a row in the tableview in that view controller, they get sent to DetailController
tableRowData = filteredBooks[indexPath.row]
self.performSegue(withIdentifier: "toDetail", sender: nil)
DetailControler has a navigation bar that I can't seem to remove. I tried to add the following code to viewWillAppear, but it made no difference. What can I do?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true
refresh()
}
I have tried your code and found that if you set hidesNavigationBarDuringPresentation to false, the DetailController's navigationBar will be hidden correctly. Here is my code
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchBar.searchBarStyle = UISearchBar.Style.prominent
searchController.searchBar.isTranslucent = false
searchController.hidesNavigationBarDuringPresentation = false

PSTCKPaymentCardTextField not displaying on view

Trying to integrate Paystack with my app. Using PSTCKPaymentCardTextField as described in the guide, but it just doesn't display anything on my view when I run the app.
import UIKit
import Paystack
class PaymentViewController: UIViewController, PSTCKPaymentCardTextFieldDelegate {
let paymentTextField: PSTCKPaymentCardTextField = {
let ptf = PSTCKPaymentCardTextField()
return ptf
}()
func setupPaymentTextField() {
let paymentFormWidth = self.view.frame.width - 30
paymentTextField.frame = CGRect(x: 15, y: 15, width: paymentFormWidth, height: 44)
paymentTextField.delegate = self
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(paymentTextField)
setupPaymentTextField()
}
}
Am i doing something wrong? Please help me out.
Thanks
Note: I am new to swift language
Issue solved!. Used auto-layout instead of using paymentTextField.frame = CGRect(x: 15, y: 15, width: paymentFormWidth, height: 44)
func setupPaymentTextField() {
paymentTextField.delegate = self
paymentTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
paymentTextField.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
paymentTextField.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -30).isActive = true
paymentTextField.heightAnchor.constraint(equalToConstant: 45).isActive = true
}

Swift iOS - Tag collection view

I'm writing my first iOS app and I wanna just answer what is the best-known solution to make this? It's simple tag collection. I have already looked over the Internet but I have found nothing. I think the best way is to make my own structure of buttons maybe?
Here is what I want to achieve:
sometimes you need do it yourself:
import UIKit
import PlaygroundSupport
class TagsView: UIView {
// MARK: - Properties
var offset: CGFloat = 5
// MARK: - Public functions
func create(cloud tags: [UIButton]) {
var x = offset
var y = offset
for (index, tag) in tags.enumerated() {
tag.frame = CGRect(x: x, y: y, width: tag.frame.width, height: tag.frame.height)
x += tag.frame.width + offset
let nextTag = index <= tags.count - 2 ? tags[index + 1] : tags[index]
let nextTagWidth = nextTag.frame.width + offset
if x + nextTagWidth > frame.width {
x = offset
y += tag.frame.height + offset
}
addSubview(tag)
}
}
}
private func button(with title: String) -> UIButton {
let font = UIFont.preferredFont(forTextStyle: .headline)
let attributes: [NSAttributedString.Key: Any] = [.font: font]
let size = title.size(withAttributes: attributes)
let button = UIButton(type: .custom)
button.setTitle(title, for: .normal)
button.titleLabel?.font = font
button.setTitleColor(.darkGray, for: .normal)
button.layer.borderWidth = 1.0
button.layer.cornerRadius = size.height / 2
button.layer.borderColor = UIColor.darkGray.cgColor
button.frame = CGRect(x: 0.0, y: 0.0, width: size.width + 10.0, height: size.height + 10.0)
button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 5.0, bottom: 0.0, right: 5.0)
return button
}
let titles = ["Freedom", "God", "Happiness", "Imagination", "Intelligence", "Other"]
let tags = titles.map { button(with: $0) }
let frame = CGRect(x: 0, y: 0, width: 260, height: 200)
let tagsView = TagsView(frame: frame)
tagsView.backgroundColor = .white
tagsView.create(cloud: tags)
PlaygroundPage.current.liveView = tagsView
PlaygroundPage.current.needsIndefiniteExecution = true
I resolve this problem, using collection view.
class FilterController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
#IBOutlet var collectionView: UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 150, left: 10, bottom: 150, right: 10)
// layout.itemSize = CGSize(width: 90, height: 45)
layout.itemSize = CGSizeFromString("Aloha")
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(TagCell.self, forCellWithReuseIdentifier: "TagCell")
collectionView!.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView!)
}
Just dynamically add buttons to the superView, and change the background as per your requirement.

Resources