I want to display IronSource ads in my Swift app. i can see the ad at bottom which is by default but i want to show ads at customise place not at bottom , like i did for AdMob and Meta Ads. but this time its not working ..
this is my codes..
var bannerView: ISBannerView! = nil
and this is my UIView outlet in VC.
#IBOutlet weak var banner: UIView!
in View did Load
let BNSize: ISBannerSize = ISBannerSize(description: "LARGE",width:320 ,height:90)
IronSource.loadBanner(with: self, size: BNSize )
and then in func my code is something like this ..
func bannerDidLoad(_ bannerView: ISBannerView!) {
self.bannerView=bannerView
if #available(iOS 11.0, *) {
bannerView.frame = CGRect(x: view.frame.size.width/2 - bannerView.frame.size.width/2, y: view.frame.size.height - bannerView.frame.size.height, width: bannerView.frame.size.width, height: bannerView.frame.size.height - self.view.safeAreaInsets.bottom * 2.5)
} else {
bannerView.frame = CGRect(x: view.frame.size.width/2 - bannerView.frame.size.width/2, y: view.frame.size.height - bannerView.frame.size.height, width: bannerView.frame.size.width, height: bannerView.frame.size.height * 2.5)
}
view.addSubview(bannerView)
logFunctionName()
}
i have tried this to display ads in my UIView
view.addSubview(banner)
but it did not work tried to remove CGRect also did not work for me , i have also tried change self.bannerView=bannerView to self.bannerView=banner and vice versa, but still not working , please can you help me out ?
Related
I want to implement AppLovin Max ads in my app , previously i was working with adMob , but now the problem is i don’t know how to implement ads in custom UIView , i can display ads at bottom but i want to show ads inside the post,, here is the link to display ad
func createBannerAd()
{
adView = MAAdView(adUnitIdentifier: “”)
adView.delegate = self
// Banner height on iPhone and iPad is 50 and 90, respectively
let height: CGFloat = (UIDevice.current.userInterfaceIdiom == .pad) ? 90 : 90
let theHeight = view.frame.size.height //grabs the height of your view
// Stretch to the width of the screen for banners to be fully functional
let width: CGFloat = UIScreen.main.bounds.width
adView.frame = CGRect(x: 0, y: theHeight - 70, width: width, height: height)
// Set background or background color for banners to be fully functional
adView.backgroundColor = .white
view.addSubview(adView)
// Load the first ad
adView.loadAd()
}
how can i add this ad to my custom UIView ?
i have tried like this
func createBannerAd()
{
adView = MAAdView(adUnitIdentifier: “”)
adView.delegate = self
adView.backgroundColor = .white
view.addSubview(customUIVIEW)
adView.loadAd()
}
but it did not work , please help
I don't understand why the iPhone 4,7 is displayed correctly, but on iphone 5,5 is not displayed correctly. I want make paging by ScrollView.
What should I do to be displayed correctly on all devices?
func showScrollView() {
var photoSlides = [Slides]()
for image in photo {
let slide: Slides = Bundle.main.loadNibNamed("Slide", owner: self, options: nil)?.first as! Slides
slide.slideImage.image = image.image
photoSlides.append(slide)
}
setupSlideScrollView(slides: photoSlides)
}
func setupSlideScrollView(slides : [Slides]) {
scrollView.contentSize = CGSize(width: scrollView.frame.width * CGFloat(slides.count), height: 200)
scrollView.isPagingEnabled = true
for i in 0 ..< slides.count {
slides[i].frame = CGRect(x: scrollView.frame.width * CGFloat(i), y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
scrollView.addSubview(slides[i])
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/scrollView.frame.width)
pageControl.currentPage = Int(pageIndex)
}
https://yadi.sk/i/ipS5WchP3aRtZ9 - correctly (iPhone 8)
https://yadi.sk/i/SPmriRFn3aRtgL - not correctly (iPhone 8 Plus)
If you have used storyboard or xib to design your UI, and then for some portion of your screen you want frame size to calculate frame of other controls then you have to use layoutIfNeeded method on that control. i.e in your case you can do something like below
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
scrollView.layoutIfNeeded()
// THEN CALL YOUR FUNCTION WHICH SETUP YOUR OTHER VIEWS
showScrollView()
}
I'm trying to integrate AdMob into my Swift-based app but it doesn't work no-matter what code I try.
I created a new View Controller and used the exact same code as my 'Home' controller and that seems to work, but it will not work on any of the existing ViewControllers that I have.
My current storyboard looks like this:
and my current Ad code looks like this:
import UIKit
import GoogleMobileAds
import AudioToolbox
class AdTest: UIViewController, GADBannerViewDelegate {
// Ad banner
var adMobBannerView = GADBannerView()
let ADMOB_BANNER_UNIT_ID = "ca-app-pub-9999999999"
override func viewDidLoad() {
super.viewDidLoad()
// Init AdMob banner
initAdMobBanner()
}
// MARK: - ADMOB BANNER
func initAdMobBanner() {
if UIDevice.current.userInterfaceIdiom == .phone {
// iPhone
adMobBannerView.adSize = GADAdSizeFromCGSize(CGSize(width: 320, height: 50))
adMobBannerView.frame = CGRect(x: 0, y: view.frame.size.height, width: 320, height: 50)
} else {
// iPad
adMobBannerView.adSize = GADAdSizeFromCGSize(CGSize(width: 468, height: 60))
adMobBannerView.frame = CGRect(x: 0, y: view.frame.size.height, width: 468, height: 60)
}
adMobBannerView.adUnitID = ADMOB_BANNER_UNIT_ID
adMobBannerView.rootViewController = self
adMobBannerView.delegate = self
view.addSubview(adMobBannerView)
let request = GADRequest()
adMobBannerView.load(request)
}
// Hide the banner
func hideBanner(_ banner: UIView) {
UIView.beginAnimations("hideBanner", context: nil)
banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height - banner.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
UIView.commitAnimations()
banner.isHidden = true
}
// Show the banner
func showBanner(_ banner: UIView) {
UIView.beginAnimations("showBanner", context: nil)
banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height - banner.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
UIView.commitAnimations()
banner.isHidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
My AdTest ViewController shows this:
Whereas my Home ViewController doesnt show ads at all:
So I found the issue, turns out that AdMob will not work if you change the global user defaults.
As a temporary fix, i've added this right before the ad is shown.
//MARK : - Load table data
UserDefaults.standard.register(defaults: ["UserAgent": "AB-" + UIDevice.current.identifierForVendor!.uuidString])
////////////
//GET CODE//
////////////
//MARK : - Load AdMob
UserDefaults.standard.register(defaults: ["UserAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Mobile/15A372"])
initAdMobBanner()
//Set browser back so the app works
UserDefaults.standard.register(defaults: ["UserAgent": "AB-" + UIDevice.current.identifierForVendor!.uuidString])
I am trying to use this plugin as refresh action : https://github.com/entotsu/PullToBounce
One, issue is I can't understand his explanation.
Explanation given on the github
tableView.frame = yourFrame --> tableView is equal to scrollView.frame in my situation
yourFrame --> I have no idea what it is. The main frame ? Another Frame I have to create ?
bodyView.addSubview(tableViewWrapper) --> bodyView ? Main Frame here ? or Another frame ?
Here is my code for the scrollView for now. Any help on how to implement this plugin using a scrollView made via the storyboard.
class ProfileViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
func makeMock() {
let headerView = UIView()
headerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 64)
headerView.backgroundColor = UIColor.lightBlue
self.view.addSubview(headerView)
let headerLine = UIView()
headerLine.frame = CGRect(x: 0, y: 0, width: 120, height: 8)
headerLine.layer.cornerRadius = headerLine.frame.height/2
headerLine.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.8)
headerLine.center = CGPoint(x: headerView.frame.center.x, y: 20 + 44/2)
headerView.addSubview(headerLine)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blue
let bodyView = UIView()
bodyView.frame = scrollView.frame
bodyView.frame.y += 20 + 44
self.view.addSubview(bodyView)
let tableViewWrapper = PullToBounceWrapper(scrollView: scrollView)
bodyView.addSubview(tableViewWrapper)
tableViewWrapper.didPullToRefresh = {
NSTimer.schedule(delay: 2) { timer in
tableViewWrapper.stopLoadingAnimation()
}
}
makeMock()
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
One thing, I notice is that there is a View on top of my scrollView that disable me to view it and scroll it. Help here needed please.
Regards,
Hary
Take a look at the Example of this library.
yourFrame is nothing but your tableview class. For example if your tableView Class is named SampleTableView, then it goes like
let tableView = SampleTableView(frame: self.view.frame, style: UITableViewStyle.Plain).
You have to use another class to set up your tableView.
I'm making a very simple app for a demo and am trying to present a webpage using SFSafariViewController (I need to use SF versus WKWebView so to be able to access cookies).
I would really like to present the User with some UI buttons, but I've been unable to pull it off.
I tried this snippet (placed in the completion callback of presentViewController():
let width: CGFloat = 66
let x: CGFloat = self.view.frame.width - width
// It can be any overlay. May be your logo image here inside an imageView.
let overlay = UIView(frame: CGRect(x: x, y: 20, width: width, height: 44))
overlay.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
svc.view.addSubview(overlay)
... outlined in this post. In their case, they're attempting to cover the reload button with a small view. Regardless of the use-case, for me the view immediately disappears when I load SFSafariViewController (I can see it for a moment and it disappears).
I was thinking about presenting the button in an .OverContext modal, but then the User would be unable to interact with the SFSafariViewController, which also doesn't work.
Here's essentially what I'm after (pardon the gross, quick mockup) ... basically, SafariViewController with a view presented over it (see bottom) ... the transparency is just to show that it's being presented over Safari).
Any recommendations are greatly appreciated.
Figured it out ... there's likely some slight race condition going on that was preventing the recommended "draw a rectangle" code from working as desired. What I did:
Subclassed SFSafariWebViewController
In viewDidAppear, implemented a slight delay using NSTimer that draws any additional view elements
This also ended up helping me hide the status bar, which was giving me issues (see mockup).
Here's the relevant code:
import UIKit
import SafariServices
class MySafariVC: SFSafariViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var frame = self.view.frame
let OffsetY: CGFloat = 44
frame.origin = CGPoint(x: frame.origin.x, y: frame.origin.y - OffsetY)
frame.size = CGSize(width: frame.width, height: frame.height + (1 * OffsetY))
self.view.frame = frame
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "drawNavBar", userInfo: nil, repeats: false)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("i laid out my subviews")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func drawNavBar() {
let height: CGFloat = 44
let y: CGFloat = self.view.frame.height - height
// It can be any overlay. May be your logo image here inside an imageView.
let overlay = UIView(frame: CGRect(x: 0, y: y, width: self.view.frame.width, height: height))
overlay.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.9)
self.view.addSubview(overlay)
}
}