티스토리 뷰

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
49
50
import UIKit
 
class ViewController: UIViewController ,UITextViewDelegate{
 
    var contentField: UITextView?
    let gap:CGFloat = 8 //TextView 와 view 사이 gap
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) //UIKeyboardWillShowNotification 발생시 keyboardWillShow함수 호출
    }
    
    override func viewDidAppear(animated: Bool) {
        contentField = UITextView(frame: CGRectMake(gap, gap, self.view.frame.width, 1))
        contentField?.delegate = self
        self.view.addSubview(contentField!// 키보드 활성화를 위해 TextView 생성후 view에 추가
        contentField?.becomeFirstResponder() //키보드 활성화
    }
    
    func keyboardWillShow(notification:NSNotification) { //키보드 활성화시 호출
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        let keyboardHeight = keyboardRectangle.height //키보드 높이
        
        print("키보드 높이 : \(keyboardHeight)")
        self.setTextView(keyboardHeight)
    }
 
    func setTextView(keyboardHeight:CGFloat){
        var naviHeight:CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height //statusBar 높이
        if(self.navigationController != nil){
            naviHeight += (self.navigationController?.navigationBar.frame.size.height)!//네비게이션바 높이
        }
        
        print("네비바 + statusbar 높이: \(naviHeight)")
        let gapY = self.view.frame.height - keyboardHeight - naviHeight - (gap * 2)
        let gapX = self.view.frame.width - (gap * 2)
        contentField?.removeFromSuperview() //키보드 활성화를 위해 만들었던 TextView 삭제
        contentField = UITextView(frame: CGRectMake(gap, naviHeight + gap, gapX, gapY))
        contentField?.backgroundColor = UIColor.grayColor() //TextView 배경색 설정
        contentField?.delegate = self
        self.view.addSubview(contentField!//뷰에 추가
        contentField?.becomeFirstResponder() //키보드 활성화
    }
}
 
 
 
 
cs





댓글
댓글쓰기 폼