There is a very weird bug when I push a control to navigation controller and when I return back, look at the gif below.
Before push the detail view controller
After click on back button from detail view controller
My code:
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = Localizations.searching
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.showsScopeBar = true
viewController.navigationItem.searchController = searchController
viewController.navigationItem.hidesSearchBarWhenScrolling = false
viewController.definesPresentationContext = true
For the moment I have found a solution but that looks bad.
After push the viewcontroller I must run this:
let searchedText = searchController.searchBar.text
searchController.isActive = false
searchController.searchBar.text = searchedText
Had the same Problem and found a feasible solution:
After you add the TableView to your ViewController, pinn all directions to the safeAreaLayout(or superview)
self.view.addSubview(tableView)
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
And activate Extend Edges - Unter Opaque Bars
Related
I am trying to create a uiview that has a segment control inside. I want to be able to add this uiview to my viewcontroller's view. the segment control should be right on top of my tableview. but everytime i setup the constraints i keep getting this error
"Thread 1: Exception: "Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x282ee24c0 "UIView:0x119d3a610.bottom"> and <NSLayoutYAxisAnchor:0x282ee2500 "UITableView:0x11a014a00.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal."" I tried working around by adding the subview first and what not but it's not working. here's my code if anyone can help me.
func configureTableView(){
setupSegmentControl()
view.addSubview(tableView)
setTableViewDelegates()
tableView.rowHeight = 50
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
self.tableView.topAnchor.constraint(equalToSystemSpacingBelow: self.view.topAnchor, multiplier: 20).isActive = true
tableView.register(UINib(nibName: "CustomCellNSB2", bundle: nil), forCellReuseIdentifier: "CustomCellNSB2")
}
func setTableViewDelegates(){
tableView.delegate = self
tableView.dataSource = self
}
func setupSegmentControl(){
var headerView = UIView()
var importanceSegmentControl = CustomSegmentControl()
headerView.addSubview(importanceSegmentControl)
self.view.addSubview(headerView)
importanceSegmentControl.addTarget(self, action: #selector(indexChanged(control:)),for: UIControl.Event.valueChanged)
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20).isActive = true
headerView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20).isActive = true
headerView.bottomAnchor.constraint(equalTo: self.tableView.topAnchor, constant: 20).isActive = true
headerView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 10).isActive = true
importanceSegmentControl.translatesAutoresizingMaskIntoConstraints = false
importanceSegmentControl.leadingAnchor.constraint(equalTo: headerView.leadingAnchor, constant: 20).isActive = true
importanceSegmentControl.trailingAnchor.constraint(equalTo: headerView.trailingAnchor, constant: -20).isActive = true
importanceSegmentControl.bottomAnchor.constraint(equalTo: headerView.topAnchor, constant: 20).isActive = true
importanceSegmentControl.topAnchor.constraint(equalTo: headerView.topAnchor, constant: 10).isActive = true
}
The tableView and importanceSegmentControl doesn't have any common ancestor at the time of adding the constraint to the importanceSegmentControl. So to fix the issue just switch the order of execution:
func configureTableView(){
view.addSubview(tableView)
setupSegmentControl()
//...
}
I have a UITableView in my storyboard. I am trying to setup constraints for it in my view controller. After running the application it's not showing up at all. it's only working when i don't run the constraints. here are my constraints.
func tableviewsConstraints(){
homeTableView.translatesAutoresizingMaskIntoConstraints = false
homeTableView.layer.cornerRadius = 10
homeTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20).isActive = true
homeTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20).isActive = true
homeTableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
homeTableView.heightAnchor.constraint(equalToConstant: homeTableView.rowHeight * 3).isActive = true
self.view.addSubview(homeTableView)
}
override func viewDidLoad() {
super.viewDidLoad()
tableviewsConstraints()
Add the homeTableView as the view's subview before adding the constraints to it. And use a constant value when setting the height of tableView, i.e.
func tableviewsConstraints(){
self.view.addSubview(homeTableView) //here....
homeTableView.layer.cornerRadius = 10
homeTableView.translatesAutoresizingMaskIntoConstraints = false
homeTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20).isActive = true
homeTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -20).isActive = true
homeTableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
let rowHeight: CGFloat = 100.0 //here....
homeTableView.heightAnchor.constraint(equalToConstant: rowHeight * 3).isActive = true
}
Change the rowHeight value as per your requirement.
Don't you have the outlet of the table view if you have it in the storyboard? Then why you need to subview? If you're creating table view programmatically then follow this.
What I am trying to achieve is to have a button click action trigger a uiview popup with a search bar at the top of the view, which then displays a UISearchBar and a UITableView below. I have been successful at implementing these events, but when clicking inside the search bar nothing happens. The search bar just has the word search and a search bar button. I have subclassed the UISearchBarDelegate, UISearchResultsUpdating, and UISearchControllerDelegate for the ViewController. As mentioned, I am doing this programmatically in Swift without a storyboard. There are no outlets or actions. I am calling methods to perform the necessary actions.
private var resultSearchController: UISearchController!
private var locationSearchTable = LocationSearchTable()
func didClickSearchButton() {
locationSearchTable = LocationSearchTable()
locationSearchTable.tableView.delegate = self
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
mapSearchTable.tableHeaderView = resultSearchController?.searchBar
resultSearchController.delegate = self
resultSearchController.searchBar.delegate = self
resultSearchController.searchResultsUpdater = self
}
I am adding the searchBar to the tableView header and it does show up.
override func viewDidLoad() {
super.viewDidLoad()
layout()
}
Calling the 'layout' in the SuperView works.
private func layout() {
popupView.translatesAutoresizingMaskIntoConstraints = false
popupView.addGestureRecognizer(panRecognizer)
didClickSearchButton()
popupView.addSubview(resultSearchController.searchBar) //Tried adding this to the view, but all it did was put it in a different location.
popupView.addSubview(openTitleLabel)
popupView.addSubview(searchButton)
popupView.addSubview(mapSearchTable)
view.addSubview(popupView)
popupView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
popupView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
bottomConstraint = popupView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: popupOffset)
bottomConstraint.isActive = true
popupView.heightAnchor.constraint(equalToConstant: 467).isActive = true
searchButton.translatesAutoresizingMaskIntoConstraints = false
searchButton.leadingAnchor.constraint(equalTo: popupView.leadingAnchor, constant: 50).isActive = true
searchButton.trailingAnchor.constraint(equalTo: popupView.trailingAnchor, constant: -50).isActive = true
searchButton.topAnchor.constraint(equalTo: popupView.topAnchor, constant: 5).isActive = true
openTitleLabel.translatesAutoresizingMaskIntoConstraints = false
openTitleLabel.leadingAnchor.constraint(equalTo: popupView.leadingAnchor, constant: 50).isActive = true
openTitleLabel.trailingAnchor.constraint(equalTo: popupView.trailingAnchor, constant: -50).isActive = true
openTitleLabel.topAnchor.constraint(equalTo: popupView.topAnchor, constant: 5).isActive = true
openTitleLabel.bottomAnchor.constraint(equalTo: popupView.bottomAnchor, constant: 0)
mapSearchTable.translatesAutoresizingMaskIntoConstraints = false
mapSearchTable.topAnchor.constraint(equalTo: popupView.topAnchor, constant: 50).isActive = true
mapSearchTable.bottomAnchor.constraint(equalTo: popupView.bottomAnchor, constant: 10).isActive = true
mapSearchTable.leftAnchor.constraint(equalTo: popupView.leftAnchor).isActive = true
mapSearchTable.rightAnchor.constraint(equalTo: popupView.rightAnchor).isActive = true
mapSearchTable.centerXAnchor.constraint(equalTo: popupView.centerXAnchor).isActive = true
}
I can see all of the elements, but there is no focus inside the text field when clicking on the input box.
It turned out that the issue was with the alpha value that was a stored property in the UISearchBar class I created to handle the rendering of the searchBar. I had the value for the alpha set to '0' and after removing that value I was able to render the UISearchBar normally.
I created a WKWebView in code and added it to my view.
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
When the webpage opens I can see that my navigation bar from the previous view is blocking some of my webpage.
How can I adjust the view in code to make my webpage appear below my navigation bar?
You should add a top constraint to your webview and give it a constant value that works for you (in case you want to add a margin)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: parentView.topAnchor, constant: 40).isActive = true
You should also add the remaining anchor values.
webView.bottomAnchor.constraint(equalTo: parentView.bottomAnchor, constant: 0).isActive = true
webView.trailingAnchor.constraint(equalTo: parentView.trailingAnchor, constant: 0).isActive = true
webView.leadingAnchor.constraint(equalTo: parentView.leadingAnchor, constant: 0).isActive = true
I am trying to generate views displayed in a UIScrollView based on structured (XML) data at runtime. I generate all the views I need and add them to my scrollview afterwards, using Auto Layout to let the scrollview calculate its contentSize. Everything worked fine as long as I was only using UILabels and a custom UIImageView subclass, but now I want to add a nested view containing another view and a label to represent text with a vertical line on the left side.
The general layout code I'm using to add all generated views to the scrollview is as follows:
var previousAnchor = scrollView.topAnchor
views.forEach { (view) in
scrollView.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
view.topAnchor.constraint(equalTo: previousAnchor,
constant: UI.defaultPadding).isActive = true
view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor,
constant: UI.smallPadding).isActive = true
view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor,
constant: -UI.smallPadding).isActive = true
previousAnchor = view.bottomAnchor
}
scrollView.bottomAnchor.constraint(equalTo: previousAnchor,
constant: UI.defaultPadding).isActive = true
And the layout code for my nested subview looks like this (it's a UIView subclass):
addSubview(label)
addSubview(line)
line.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
line.topAnchor.constraint(equalTo: topAnchor).isActive = true
line.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
line.widthAnchor.constraint(equalToConstant: 2).isActive = true
line.trailingAnchor.constraint(equalTo: label.leadingAnchor).isActive = true
line.backgroundColor = UIColor.green
label.topAnchor.constraint(equalTo: topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
label.numberOfLines = 0
label.text = "Here's to the crazy ones. The misfits. The rebels. The troublemakers."
If I use this code, I cannot see this view in my scrollview at all and the scrollview has a warning of ambiguous content size attached to it in the Debug View Hierarchy view. This is somewhat irritating, as using labels without being nested in a view like this does work like a charm. So I started playing around and what really puzzles me is that if I use the following code I can see the view itself (as I gave it a gray background color), but still can't see line and/or label, as both seem have a frame of (0,0,0,0) upon inspection:
addSubview(label)
addSubview(line)
line.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
line.topAnchor.constraint(equalTo: topAnchor, constant: 10).isActive = true
line.widthAnchor.constraint(equalToConstant: 20).isActive = true
line.heightAnchor.constraint(equalToConstant: 20).isActive = true
line.trailingAnchor.constraint(equalTo: label.leadingAnchor).isActive = true
line.backgroundColor = UIColor.green
label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
heightAnchor.constraint(equalToConstant: 100).isActive = true
heightAnchor.constraint(equalTo: label.heightAnchor).isActive = true
label.numberOfLines = 0
label.text = "Here's to the crazy ones. The misfits. The rebels. The troublemakers."
What am I missing? I also tried putting all my views into a container view instead of using them as subviews of the scrollview directly, but this did not help with my nested view and brought up other problems.