The instruction from Google is the Storyboard method. How to show Admob banner on top without using Storyboard? I am trying to put it in the ScrollView in this code.
I tried to put this under viewDidLoad, but it does not work.
override func viewDidLoad() {
super.viewDidLoad()
var bannerView: GADBannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.adUnitID = "ca-app-pub-MYID"
bannerView.rootViewController = self
bannerView.loadRequest(GADRequest())
view.addSubview(bannerView)
mainScrollView = UIScrollView(frame: self.view.bounds)
mainScrollView.pagingEnabled = true
mainScrollView.showsHorizontalScrollIndicator = false
mainScrollView.showsVerticalScrollIndicator = false
pageScrollViews = [UIScrollView?](count: photos.count, repeatedValue: nil)
let innerScrollFrame = mainScrollView.bounds
mainScrollView.contentSize =
CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width,
mainScrollView.bounds.size.height)
print(mainScrollView.contentSize)
mainScrollView.backgroundColor = UIColor.blackColor()
mainScrollView.delegate = self
self.view.addSubview(mainScrollView)
configScrollView()
addPageControlOnScrollView()
}
you forgot to add this line in code
banerView.frame = cgrectmake(0,430,320,50);
[self.view addsubView:bannerView];
Problem solved by adding Admob under addPageControlOnScrollView.
func addPageControlOnScrollView() {
self.pageControl.numberOfPages = photos.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.redColor()
self.pageControl.pageIndicatorTintColor = UIColor.whiteColor()
self.pageControl.currentPageIndicatorTintColor = UIColor.greenColor()
pageControl.addTarget(self, action: Selector("changePage:"), forControlEvents: UIControlEvents.ValueChanged)
self.pageControl.frame = CGRectMake(0, self.view.frame.maxY - 84, self.view.frame.width, 44)
//self.pageControl.backgroundColor = UIColor.yellowColor()
self.view.addSubview(pageControl)
bannerView.adUnitID = "ca-app-pub-MYID"
bannerView.rootViewController = self
bannerView.loadRequest(GADRequest())
view.addSubview(bannerView)
}
Related
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
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()
}
I have an issue with UITextField that do not respond to the touch events when it's added to ContainerView. When I try to add UITextField to the main UIViewController it seems to be fine and it responds (keyboard is showing).
I tried to bring the subview to the front, enabled user interaction on ContainerView - nothing helped.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.scrollView = UIScrollView()
self.containerView = UIView()
self.scrollView.delegate = self
self.containerView.userInteractionEnabled = true
self.scrollView.addSubview(containerView)
self.view.addSubview(scrollView)
registerForKeyboardNotifications()
setupGesturesAndMap()
setupActivityIndicator()
setupCircleButton()
setupBrowseTextField()
// Initializing status
status = Status.Init
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = self.view.bounds
containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)
}
and there is function responsible for setup of browseTextField:
func setupBrowseTextField() {
// Setup of browseTextField
browseTextField = UITextField(frame: CGRectMake(10, self.view.frame.height / 2, self.view.frame.width - 20, self.view.frame.height / 12))
browseTextField.placeholder = "Enter your destination here"
browseTextField.font = UIFont.systemFontOfSize(15)
browseTextField.borderStyle = UITextBorderStyle.RoundedRect
browseTextField.autocorrectionType = UITextAutocorrectionType.No
browseTextField.keyboardType = UIKeyboardType.Default
browseTextField.returnKeyType = UIReturnKeyType.Done
browseTextField.clearButtonMode = UITextFieldViewMode.WhileEditing
browseTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
browseTextField.delegate = self
containerView.addSubview(browseTextField)
}
Would anyone of you have any suggestions?
I see you have set the frame of the containerView as
containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)
scrollView.contentSize.width is 0.
This solves your problem.
containerView.frame = self.view.bounds
Also your can give the containerView size by the frame size of the scrollView as
containerView.frame = CGRectMake(0, 0, scrollView.frame.width, scrollView.frame.height)
I need a UIScrollView and a containerView to scroll the content I create. My code for that 2 items is:
var scrollView = UIScrollView()
var darkener = UIView()
var container = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.clearColor()
darkener = UIView(frame: self.view.frame)
darkener.backgroundColor = UIColor.blackColor()
darkener.alpha = 0.0
self.view.addSubview(darkener)
scrollView.frame = self.view.bounds
scrollView.backgroundColor = UIColor.clearColor()
scrollView.contentSize = CGSizeMake(scrollView.frame.width, scrollView.frame.height+1)
scrollView.delegate = self
self.view.addSubview(scrollView)
container = UIView(frame: CGRectMake(0, scrollView.frame.height, scrollView.frame.width, scrollView.frame.height))
container.backgroundColor = UIColor.whiteColor()
container.layer.masksToBounds = true
container.layer.cornerRadius = 10
scrollView.addSubview(container)
}
The problem is that the container view once I Set the Y point to scrollView.frame.height doesn't appear on the screen (it's on the bottom), but in a previous VC that code worked perfectly.
Also, it does not let me scroll on the content. What's wrong there?
If necessary I can upload the implementation, but this I think has no problem.
Solved. I just had to put that at the end of the ViewDidLoad to declare de frame:
The textField is the final label I have on the screen, to declare the limit of the screen.
self.container.frame = CGRectMake(0, self.container.frame.origin.y, self.container.frame.width, textField2.frame.origin.y + textField2.frame.height - 60)
self.scrollView.contentSize = CGSizeMake(self.container.frame.width, self.container.frame.height + 20.0)
And works Ok now.
Am trying to implement search nearby places using google maps. The below is the code what I have done so far
override func viewDidLoad() {
super.viewDidLoad()
//Adding Mapview
mapView = GMSMapView(frame: CGRectMake(0, 120, self.view.bounds.width, self.view.bounds.height - 120))
self.view.addSubview(mapView)
mapView.mapType = kGMSTypeNormal
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let resultTableView = UITableView(frame: CGRectMake(10, 100, 300, 60))
self.searchResultController = UITableViewController()
self.searchResultController?.tableView = resultTableView
self.searchResultController?.tableView.dataSource = self
self.searchResultController?.tableView.delegate = self
self.searchResultController?.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.searchController = UISearchController(searchResultsController: self.searchResultController)
self.searchController?.searchResultsUpdater = self
self.searchController?.hidesNavigationBarDuringPresentation = true
self.searchController?.dimsBackgroundDuringPresentation = false
self.searchController?.delegate = self
self.searchController?.searchBar.frame = CGRectMake(0,70,self.view.bounds.width,40)
self.searchController?.searchBar.placeholder = "Please choose a location"
self.view.addSubview(self.searchController!.searchBar)
self.definesPresentationContext = true
}
Everything works fine, except searcg bar disappears after a single tap. But it does its function.
I found the solution for my above problem.
func willPresentSearchController(searchController: UISearchController){
self.searchResultController?.tableView.addSubview(self.searchController!.searchBar)
}
Hope it helps to others!!!
Try calling [[[self searchController] searchBar] sizeToFit]; inside layoutSubviews method. It helped me.