티스토리 뷰
애드몹 코드를 구글에서 제공하는 대로 가져다쓰면 문제가 딱 두 가지 존재한다. 첫번째는 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)
])
}
'Dev > iOS' 카테고리의 다른 글
Tip : 해당 URL로 이동하기 (0) | 2021.01.11 |
---|---|
Tip : 코드로 custom color 사용하기 (0) | 2021.01.11 |
[Dev, iOS] iOS 14+ widget의 view를 바로바로 업데이트 하기 (0) | 2021.01.11 |
Tip : 탭바 컨트롤러 위 테두리를 없애고 싶을 때 (0) | 2021.01.06 |
Tip : 배열 범위 내 랜덤 변수 뽑기 (0) | 2020.12.19 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- CSV
- D3
- 자료구조
- how to start without storyboard
- BOJ
- CSS
- 알고리즘
- DFS
- Javascript
- TIP
- how to remove border of tabbarcontroller
- pyrebase
- 하노이탑이동순서
- 곱셈의 역원
- Python
- ios
- Array
- PS
- 정보시각화
- 파이썬
- 백트래킹
- SVG
- HTML
- SceneDelegate
- c++
- swift
- 백준
- 보라매사옥
- DP
- xcode
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함