티스토리 뷰

애드몹 코드를 구글에서 제공하는 대로 가져다쓰면 문제가 딱 두 가지 존재한다. 첫번째는 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)
            ])
         }
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함