[Dev, iOS] Google Admob 배너 위치 변경하기

2021. 1. 11. 12:04·Dev/iOS
728x90

애드몹 코드를 구글에서 제공하는 대로 가져다쓰면 문제가 딱 두 가지 존재한다. 첫번째는 bottomLayoutGuide가 사라졌다는 것, 그리고 코드로 UI를 짜지 않는 개발자들에게 애드몹 위치를 옮기는 것이 쉬운 일이 아니다.

 

먼저 bottomLayoutGuide로 인한 노랑 불 문제는 toItem: view.safeAreaLayoutGuide로 변경해주면 된다.

func addBannerViewToView(_ bannerView: GADBannerView) {
          bannerView.translatesAutoresizingMaskIntoConstraints = false
          view.addSubview(bannerView)
          view.addConstraints(
            [NSLayoutConstraint(item: bannerView,
                                attribute: .bottom,
                                relatedBy: .equal,
                                toItem: view.safeAreaLayoutGuide, // 이 부분
                                attribute: .bottom,
                                multiplier: 1,
                                constant: 0),
             NSLayoutConstraint(item: bannerView,
                                attribute: .centerX,
                                relatedBy: .equal,
                                toItem: view,
                                attribute: .centerX,
                                multiplier: 1,
                                constant: 0)
            ])
         }

 

위치는 어떻게 변경하느냐? 스토리보드를 사용하자.

 

1. 원하는 위치에 빈 imageView의 크기를 맞춰 layout을 해준다.

(smartBanner를 사용하는 경우 imageView의 좌우를 꽉 채우고 height을 50으로 설정해주면 된다.)

 

2. 해당 imageView를 코드에 끌어다 weak var를 설정해주고

 

3. 아래나 위로 애드몹 위치를 고정시켜주면 된다.

 

다음은 해당 코드다.

func addBannerViewToView(_ bannerView: GADBannerView) {
          bannerView.translatesAutoresizingMaskIntoConstraints = false
          view.addSubview(bannerView)
          view.addConstraints(
            [NSLayoutConstraint(item: bannerView,
                                attribute: .bottom,
                                relatedBy: .equal,
                                toItem: adView, // adView는 내가 설정한 imageView의 이름
                                attribute: .bottom, // 해당 adView와 애드몹의 bottom을 같게 만들어 줌
                                multiplier: 1,
                                constant: 0),
             NSLayoutConstraint(item: bannerView,
                                attribute: .centerX,
                                relatedBy: .equal,
                                toItem: view,
                                attribute: .centerX,
                                multiplier: 1,
                                constant: 0)
            ])
         }
728x90

'Dev > iOS' 카테고리의 다른 글

[Dev, iOS] 해당 URL로 이동하기  (0) 2021.01.11
[Dev, iOS] 코드로 custom color 사용하기  (0) 2021.01.11
[Dev, iOS] iOS 14+ widget의 view를 바로바로 업데이트 하기  (0) 2021.01.11
[Dev, iOS] 탭바 컨트롤러 위 테두리를 없애고 싶을 때  (0) 2021.01.06
[Dev, iOS] 배열 범위 내 랜덤 변수 뽑기  (2) 2020.12.19
'Dev/iOS' 카테고리의 다른 글
  • [Dev, iOS] 해당 URL로 이동하기
  • [Dev, iOS] 코드로 custom color 사용하기
  • [Dev, iOS] iOS 14+ widget의 view를 바로바로 업데이트 하기
  • [Dev, iOS] 탭바 컨트롤러 위 테두리를 없애고 싶을 때
100두산
100두산
출발하게 만드는 힘이 동기라면, 계속 나아가게 만드는 힘은 습관이다.
  • 100두산
    정상에서 보자 ✈️
    100두산
  • 전체
    오늘
    어제
    • 분류 전체보기 (126)
      • Life (6)
        • living (1)
      • Research (6)
      • AI (20)
      • Dev (45)
        • iOS (28)
        • Web (4)
        • flutter (9)
        • etc (4)
      • PS (Problem Solving) (23)
      • Computer Science and Engine.. (21)
        • Data Structures and Algorit.. (13)
        • OOP (Object Oriented Progra.. (8)
      • etc (5)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    알고리즘
    백준
    Python
    PS
    swift
    TIP
    파이썬
    AI
    자료구조
    D3
    ios
    백트래킹
    SKT
    c++
    SKTelecom
    xcode
    BOJ
    Challenger
    티스토리챌린지
    오블완
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
100두산
[Dev, iOS] Google Admob 배너 위치 변경하기
상단으로

티스토리툴바