๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UI

[iOS/UI] ์„œ์น˜๋ฐ” ์ปค์Šคํ…€ํ•˜๊ธฐ (Custom UISearchBar)

by Fomagran ๐Ÿ’ป 2020. 9. 2.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”! Foma์ž…๋‹ˆ๋‹ค!

์ด๋ฒˆ์—๋Š” ์„œ์น˜๋ฐ”์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค„๋ณผ๊ฑด๋ฐ์š”.

๊ฐ€๋” ์„œ์น˜๋ฐ”์˜ ๋ฐฐ๊ฒฝ์ƒ‰์ด๋‚˜ ๋‹๋ณด๊ธฐ์ด๋ฏธ์ง€ ๋“ฑ์„ ๋ฐ”๊พธ๊ณ ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์—ด์‹ฌํžˆ ๊ตฌ๊ธ€๋งํ•ด๋ณธ ๊ฒฐ๊ณผ

 

   1.์„œ์น˜๋ฐ”์˜ ํ”Œ๋ ˆ์ด์Šคํ™€๋”(๊ฒ€์ƒ‰ํ•˜๊ธฐ ์ „์— ํšŒ์ƒ‰๊ธ€์”จ๋กœ ๋– ์žˆ๋Š”๊ฒƒ) ์„ธํŒ…

   2.์„œ์น˜๋ฐ”์˜ ์™ผ์ชฝ ๋‹๋ณด๊ธฐ ์•„์ด์ฝ˜ ์ด๋ฏธ์ง€ ์„ธํŒ…

   3.์„œ์น˜๋ฐ”์˜ ์˜ค๋ฅธ์ชฝ ์—‘์Šค๋ฒ„ํŠผ ์ด๋ฏธ์ง€ ์„ธํŒ…(๊ฒ€์ƒ‰ํ–ˆ์„ ๋•Œ)

   4.์„œ์น˜๋ฐ”๋ฅผ ๋„ค๋น„๊ฒŒ์ด์…˜๋ฐ”์— ๋„ฃ๋Š”๋ฒ•

   5.์„œ์น˜๋ฐ”์˜ ๋ฐฐ๊ฒฝํ™”๋ฉด,๊ธ€์”จ์ƒ‰,ํ”Œ๋ ˆ์ด์Šคํ™€๋”์ƒ‰ ์„ธํŒ…

   6.์„œ์น˜๋ฐ” ์™ผ์ชฝ,์˜ค๋ฅธ์ชฝ ์ด๋ฏธ์ง€ ๋„ฃ๊ธฐ ํ‹ดํŠธ์ƒ‰ ์„ธํŒ…

 

์ด๋ ‡๊ฒŒ ์ปค์Šคํ…€ํ•˜๋Š” ๋ฒ•์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋จผ์ € ๊ฐ€์žฅ ๋จผ์ € ํ•ด์ฃผ์–ด์•ผํ•  ๊ฒƒ์€ searchbar๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. = > let searchBar = UISearchBar()

 

1.ํ”Œ๋ ˆ์ด์Šคํ™€๋” ์„ค์ •๋ฒ•์€ searchBar์— .placeholder๋ฅผ ํ•ด์ฃผ์‹œ๊ณ  ์›ํ•˜๋Š” ๋ฌธ๊ตฌ๋ฅผ ์จ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. => searchBar.placeholder = "Search"(์›ํ•˜๋Š” ๋ฌธ๊ตฌ)

 

2.์„œ์น˜๋ฐ”์˜ ์™ผ์ชฝ ๋‹๋ณด๊ธฐ ์•„์ด์ฝ˜ ์ด๋ฏธ์ง€ ์„ธํŒ…์€ .setImage๋ฅผ ํ•ด์ฃผ์‹œ๊ณ  ์ฐจ๋ก€๋Œ€๋กœ ์›ํ•˜๋Š” ์ด๋ฏธ์ง€์™€ for:UISearchBar.Icon.search(์„œ์น˜์•„์ด์ฝ˜),state๋Š” .normal๋กœ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 => searchBar.setImage(UIImage(named: "icSearchNonW"), for: UISearchBar.Icon.search, state: .normal)

 

3.์„œ์น˜๋ฐ”์˜ ์˜ค๋ฅธ์ชฝ ์—‘์Šค๋ฒ„ํŠผ ์ด๋ฏธ์ง€ ์„ธํŒ…(๊ฒ€์ƒ‰ํ–ˆ์„ ๋•Œ)  2์™€ ๋งˆ์ฐฌ์ง€์ด์ง€๋งŒ for์—๋Š” .clear(๊ฒ€์ƒ‰ํ•œ ๋ฌธ์žฅ์„ ์—†์•จ๋•Œ ์•„์ด์ฝ˜)๋กœ๋งŒ ๋ฐ”๊ฟ”์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 => searchBar.setImage(UIImage(named: "icCancel"), for: .clear, state: .normal)

 

4. ์„œ์น˜๋ฐ”๋ฅผ ๋„ค๋น„๊ฒŒ์ด์…˜๋ฐ”์— ๋„ฃ๋Š”๋ฒ•์€ ์ฃผ์˜ํ•˜์‹ค์ ์€ ๋„ค๋น„๊ฒŒ์ด์…˜ ์ฝ˜ํŠธ๋กค๋Ÿฌ๊ฐ€ ์ƒ์œ„ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์กด์žฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์กด์žฌํ•œ๋‹ค๋ฉด navigationBar์— topItem์— titleView๋ฅผ searchBar๋กœ ์„ค์ •ํ•ด์ฃผ์‹œ๋ฉด๋ฉ๋‹ˆ๋‹ค.

=> self.navigationController?.navigationBar.topItem?.titleView = searchBar

 

5.์„œ์น˜๋ฐ”์˜ ๋ฐฐ๊ฒฝํ™”๋ฉด,๊ธ€์”จ์ƒ‰,ํ”Œ๋ ˆ์ด์Šคํ™€๋”์ƒ‰ ์„ธํŒ…์€

๊ฐ€์žฅ ๋จผ์ €

=>  if let textfield = searchBar.value(forKey: "searchField"as? UITextField { ...} ๋กœ ์„œ์น˜๋ฐ”์˜ ์„œ์น˜ํ•„๋“œ ๊ฐ’์„ ๊ฐ€์ง„ textfield๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ  ๋ฐฐ๊ฒฝํ™”๋ฉด๊ณผ ๊ธ€์”จ์ƒ‰์€ ๊ฐ„๋‹จํ•˜๊ฒŒ .backgroundColor์™€ .textColor๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

ํ”Œ๋ ˆ์ด์Šคํ™€๋”๋Š” ๋”ฐ๋กœ NSAttributedString์œผ๋กœ attributes ์•ˆ์—  [NSAttributedString.Key.foregroundColor : ์›ํ•˜๋Š”์ƒ‰]์„ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

=> textfield.backgroundColor = UIColor.black (๋ฐฐ๊ฒฝํ™”๋ฉด)

     textfield.attributedPlaceholder= NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lightGray]) (ํ”Œ๋ ˆ์ด์Šคํ™€๋”)

     textfield.textColor = UIColor.white(๊ธ€์”จ์ƒ‰)

 

6.์„œ์น˜๋ฐ”์˜ ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ ์ด๋ฏธ์ง€ ๋„ฃ๊ธฐ ๋ฐ ํ‹ดํŠธ์„ค์ •์€ ๋จผ์ € ํ…์ŠคํŠธํ•„๋“œ์˜ ์™ผ์ชฝ๋ทฐ(leftView)๋ฅผ ์ด๋ฏธ์ง€๋ทฐ๋กœ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ 

leftView์˜ ์ด๋ฏธ์ง€๋ฅผ image.witheRenderingMode(.alwaysTemplate)๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ ๋‹ค์Œ ํ‹ดํŠธ ์„ค์ •์€ .tintColor = ์›ํ•˜๋Š” ์ƒ‰์œผ๋กœ ํ•ด์ค๋‹ˆ๋‹ค.

์˜ค๋ฅธ์ชฝ์€(rightView)๋กœ ํ•ด์ฃผ์‹œ๊ณ  ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

=> if let leftView = textfield.leftView as? UIImageView {

                leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)

                leftView.tintColor = UIColor.white

            }

=> if let rightView = textfield.rightView as? UIImageView {

                rightView.image = rightView.image?.withRenderingMode(.alwaysTemplate)

                rightView.tintColor = UIColor.white

            }

์ด๋ ‡๊ฒŒ ๋ชจ๋‘ ์„ค์ •ํ•ด์ฃผ์‹œ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž์‹ ๋งŒ์˜ ์ปค์Šคํ…€๋œ ์„œ์น˜๋ฐ”๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!!!

 

 

์ „์ฒด์ฝ”๋“œ

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import UIKit
 
class ViewController: UIViewController {
    
    
    override func viewDidLoad() {
        
        setSearchBar()
        
    }
    
    //์„œ์น˜๋ฐ” ์„ธํŒ…
    func setSearchBar(){
        
        //์„œ์น˜๋ฐ” ๋งŒ๋“ค๊ธฐ
        let searchBar = UISearchBar()
        searchBar.placeholder = "Search"
        //์™ผ์ชฝ ์„œ์น˜์•„์ด์ฝ˜ ์ด๋ฏธ์ง€ ์„ธํŒ…ํ•˜๊ธฐ
        searchBar.setImage(UIImage(named: "icSearchNonW"), for: UISearchBar.Icon.search, state: .normal)
        //์˜ค๋ฅธ์ชฝ x๋ฒ„ํŠผ ์ด๋ฏธ์ง€ ์„ธํŒ…ํ•˜๊ธฐ
        searchBar.setImage(UIImage(named: "icCancel"), for: .clear, state: .normal)
        //๋„ค๋น„๊ฒŒ์ด์…˜์— ์„œ์น˜๋ฐ” ๋„ฃ๊ธฐ
        self.navigationController?.navigationBar.topItem?.titleView = searchBar
        
        if let textfield = searchBar.value(forKey: "searchField"as? UITextField {
            //์„œ์น˜๋ฐ” ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ปฌ๋Ÿฌ
            textfield.backgroundColor = UIColor.black
            //ํ”Œ๋ ˆ์ด์Šคํ™€๋” ๊ธ€์”จ ์ƒ‰ ์ •ํ•˜๊ธฐ
            textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lightGray])
            //์„œ์น˜๋ฐ” ํ…์ŠคํŠธ์ž…๋ ฅ์‹œ ์ƒ‰ ์ •ํ•˜๊ธฐ
            textfield.textColor = UIColor.white
            //์™ผ์ชฝ ์•„์ด์ฝ˜ ์ด๋ฏธ์ง€๋„ฃ๊ธฐ
            if let leftView = textfield.leftView as? UIImageView {
                leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)
                //์ด๋ฏธ์ง€ ํ‹ดํŠธ์ปฌ๋Ÿฌ ์ •ํ•˜๊ธฐ
                leftView.tintColor = UIColor.white
            }
            //์˜ค๋ฅธ์ชฝ x๋ฒ„ํŠผ ์ด๋ฏธ์ง€๋„ฃ๊ธฐ
            if let rightView = textfield.rightView as? UIImageView {
                rightView.image = rightView.image?.withRenderingMode(.alwaysTemplate)
                //์ด๋ฏธ์ง€ ํ‹ดํŠธ ์ •ํ•˜๊ธฐ
                rightView.tintColor = UIColor.white
            }
 
        }
    }
    
}
cs

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€