当前位置:   article > 正文

Chapter1 基础部分 - iOS 8 Swift Programming cookBook 读书笔记_siwft preferredcontentsize

siwft preferredcontentsize

1. 给view添加模糊效果(Blur effects)

需要用到2个class:
-UIBlurEffect
-UIVisualEffectView

override func viewDidLoad() {
super.viewDidLoad()
let blurEffect = UIBlurEffect(style: .Light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame.size = CGSize(width: 200, height: 200)
blurView.center = view.center
view.addSubview(blurView)
}

其中UIBlurEffect的style有下面几种:

enum UIBlurEffectStyle : Int {
case ExtraLight
case Light
case Dark }

也可以用UIVibrancyEffect来加强对比色, t.b.d

2. PopoverViewController(两个ViewController之间的通讯)

2个view controller,按view controller A 上的加号,调出view controller B 作为segue。view controller B为一个tableview,在选择view controller B的一行的时候,view controller A要显示该行的信息。
从信息传递的角度来说,view controller A要取得view controller B的信息。

本书例子程序里面,popover是用代码写的,其实用storyboard来做,可能更符合实际情况。
Storyboard如图:
这里写图片描述
这里只有一个注意点:从加号出去的segue,要选择“present as pop”

两个viewcontroller之间的信息传递,如果用storyboard的话,需要用prepareForSegue,这个函数在view controller B显示之前调用。所以在view controller A中实现:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let nav = segue.destinationViewController as? UINavigationController {
if let destination = nav.visibleViewController as? PopoverTableViewController {
destination.selectionHandler = self.selectionHandler
}
}
}

但是由于view controller B还没有生成,没有办法在这个时候将信息传给view controller A,所以赋值给view controller B一个A的函数selectionHandler。
然后B要在条件成熟的时候调用A的函数。
selectionHandler 在 view controller A 中是一个var, ((selectedItem:String)就是需要传递的值,在didSelectRowAtIndexPath调用函数:

var selectionHandler:((selectedItem:String)->Void)?

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let selectedItem = items[indexPath]
selectionHandler?(selectedItem: selectedItem)
dismissViewControllerAnimated(true, completion: nil)
}

在view controller B中是一个函数, 取得从A过来的值,显示在TextField里面:

func selectionHandler(selecedItem: String) {
self.selecedItem = selecedItem
showUpTextField.text = selecedItem
}

另外,PopoverViewController的一些知识点:

//PopOver的大小
preferredContentSize = CGSize(width: 300, height: 200)
//在iphone上也以popover的形式呈现
首先要让view controller A实现UIPopoverPresentationControllerDelegate

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!, traitCollection: UITraitCollection!) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
在segue的时候,赋值delegate
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let nav = segue.destinationViewController as? UINavigationController {
if let destination = nav.visibleViewController as? PopoverTableViewController {
if let ppc = nav.popoverPresentationController {
ppc.delegate = self
}
destination.selectionHandler = self.selectionHandler
}
}
}

//如果popover里面是可变内容,比如textView之类的,要设置preferredContentSize为合适的值

override var preferredContentSize: CGSize {
get {
if textView != nil && presentationController != nil {
return textView.sizeThatFits(presentingViewController!.view.bounds.size)
}else {
return super.preferredContentSize
}
}
set {
super.preferredContentSize = newValue
}
}

3. 创建image view

let image = UIImage(named: “Safari”)
// ??? 为什么!可以
var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    imageView = UIImageView(frame: view.bounds)
    imageView.image = image
    imageView.contentMode = .ScaleAspectFit
    imageView.center = view.center
    view.addSubview(imageView)

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

UIImageView的contentMode属性
-UIViewContentModeScaleToFill: 拉伸图片去符合imageview的大小
-UIViewContentModeScaleAspectFit: 图片长宽比不变,图片去fit imageview的大小
-UIViewContentModeScaleAspectFill: 图片长宽比不变,图片去fill image view的大小,图片可能不完整, 要设置clipsToBounds 为True

4. 创建UILabel

var label:UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label = UILabel(frame: CGRect(x: 20, y: 100, width: 100, height: 70))
label.text = “iOS Cookbook Book”
// label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 0
label.lineBreakMode = .ByWordWrapping
label.font = UIFont.boldSystemFontOfSize(14)
view.addSubview(label)
}

5. 创建富文本的label (NSMutableAttributedString)

t.b.d

6. 创建UIButton

var button: UIButton!

func buttonIsPressed(sender: UIButton){
    print("Button is pressed.")
}

func buttonIsTapped(sender: UIButton){
    print("Button is tapped.")
}

override func viewDidLoad() {
    super.viewDidLoad()

    let normalImage = UIImage(named: "NormalBlueButton")
    let highlightedImage = UIImage(named: "HighlightedBlueButton")
    button = UIButton.buttonWithType(.Custom) as? UIButton
    button.frame = CGRect(x: 110, y: 70, width: 100, height: 44)

    button.setTitle("Press Me", forState: .Normal)
    button.setTitle("I'm Pressed", forState: .Highlighted)

    button.setBackgroundImage(normalImage, forState: .Normal)
    button.setBackgroundImage(highlightedImage, forState: .Highlighted)

    button.addTarget(self, action: "buttonIsPressed:", forControlEvents: .TouchDown)
    button.addTarget(self, action: "buttonIsTapped:", forControlEvents: .TouchUpInside)

    view.addSubview(button)
}
  • 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

7. Alart & UISwitch

t.b.d

8. UIPickerView

初始化:

var picker:UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
picker = UIPickerView()
picker.center = view.center
picker.delegate = self
view.addSubview(picker)

实现UIPickerViewDelegate 和UIPickerViewDataSource的delegate:
-numberOfComponentsInPickerView:有多少个组件
-numberOfRowsInComponent:每个组件有多少行
-titleForRow:每一行的title

在按下button后,用selectedRowInComponent函数取得用户选择的值
-picker.selectedRowInComponent(0)
如果picker的title是实时变化的,用这个函数做reload:
-picker.reloadAllComponents()

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if pickerView == picker {
return 1
}
return 0
}

func pickerView(pickerView: UIPickerView,
    numberOfRowsInComponent component: Int) -> Int {

        if pickerView == picker {
            return 10
        }
    return 0
}

func pickerView(pickerView: UIPickerView,
    titleForRow row: Int,
    forComponent component: Int) -> String! {
    return "\(row + 1)"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

9. UIDatePicker

根据datePickerMode的值不同,UIDatePicker 有4个样式:
-* .Time: 显示小时,分钟,上下午*
-* .Date: 显示年,月,日*
-* .DateAndTime: 显示Time和Date*
-* .CountDownTimer: 显示计时器,小时和分钟*

var datePicker: UIDatePicker!

override func viewDidLoad() {
    super.viewDidLoad()
    datePicker = UIDatePicker()
    datePicker.center = view.center
    datePicker.datePickerMode = .Date
    view.addSubview(datePicker)

    let oneYearTime:NSTimeInterval = 365 * 24 * 60 * 60
    let todayDate = NSDate()

    let oneYearFromToday = todayDate.dateByAddingTimeInterval(oneYearTime)
    let twoYearFromToday = todayDate.dateByAddingTimeInterval(oneYearTime * 2)
    datePicker.minimumDate = oneYearFromToday
    datePicker.maximumDate = twoYearFromToday
    datePicker.addTarget(self, action: "datePickerDateChanged:", forControlEvents: UIControlEvents.ValueChanged)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

10. UISlider

var slider: UISlider!

override func viewDidLoad() {
    super.viewDidLoad()

    slider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 23))
    slider.center = view.center
    slider.minimumValue = 0
    slider.maximumValue = 100
    slider.value = slider.maximumValue / 2.0
    //在改变值的时候有动画
    //slider.setValue(slider.maximumValue, animated: true)
    slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    slider.setThumbImage(UIImage(named: "ThumbNormal"), forState: UIControlState.Normal)
    slider.setThumbImage(UIImage(named: "ThumbHighlighted"), forState: UIControlState.Highlighted)
    //在手指离开的时候才调用action
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

// slider.continuous = false
view.addSubview(slider)
}

func sliderValueChanged(slider: UISlider) {
    println("Slider's value change to \(slider.value)")
}
  • 1
  • 2
  • 3

11. UISegmentedControl

var segmentedControl:UISegmentedControl!
let segments = NSArray(objects:
“Red”,
//32x32@2x and 16x16@1x.
UIImage(named: “blueDot”)!,
“Green”,
“Yellow”
)

override func viewDidLoad() {
    super.viewDidLoad()

    segmentedControl = UISegmentedControl(items: segments as! [AnyObject])
    segmentedControl.center = view.center
    segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    //选择之后不会保持高亮
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

// segmentedControl.momentary = true
self.view.addSubview(segmentedControl)
}

func segmentedControlValueChanged (sender:UISegmentedControl) {
    let seletedSegmentIndex = sender.selectedSegmentIndex
    let selectedSegmentText = sender.titleForSegmentAtIndex(seletedSegmentIndex)
    println("Segment \(seletedSegmentIndex) with text" + "of \(selectedSegmentText) is selected")
}
  • 1
  • 2
  • 3
  • 4
  • 5

12. UIActivityViewController

t.b.d

13. Navigation item

Navigation bar 可以显示text或者view,在title的位置。
用titleView属性来显示图片,用title来显示文字,如果是要显示view的话,view的高度不要超过128points。
添加left和right bar button item:也是可以显示文字或者view,view可以是一个其他控件,比如segment, UISwitch:

let items = [“Up”, “Down”]
let segmentedControl = UISegmentedControl(items: items)
segmentedControl.momentary = true

    segmentedControl.addTarget(self, action: "segmentedControlTapped:", forControlEvents: UIControlEvents.TouchUpInside)
    let rightBarButtonItem = UIBarButtonItem(customView: segmentedControl)
  • 1
  • 2

14 UITextField

创建一个UITextField,需要设置下列属性:
-borderStyle: 设置边框的样式
-contentVerticalAlignment: 设置内容的对齐方式
-textAlignment: 设置文字对齐方式
-text: 可读可写,读会返回当前text field里面的值

下面是一些键盘相关的属性:

  • autocapitalizationType: 可以选单词首字母大写,全大写,句子首字母大写,全部大写等;
  • autocorrectionType:自动纠错
  • enablesReturnKeyAutomatically:当是yes的时候,如果没有文字输入,return key就disable,如果有,就enable return key
  • secureTextEntry:用来输入密码
  • keyboardType:键盘类型

UITextFieldDelegate, 有下面这些delegate, 如果是在IB或者storyboard里面创建的TextField,记得要把delegate指向对应的view controller,否则delegate会不起效果:
这里写图片描述
-textFieldShouldBeginEditing: 返回Bool,表明text field是否支持编辑。False表示不能被编辑。当用户点击text field的时候,这个函数会被调用到。
-textFieldDidBeginEditing: 当用户已经在编辑text field的时候,被调用到。前提是textFieldShouldBeginEditing要返回true
-textFieldShouldEndEditing:当用户将要离开text field的时候,或者first responder正在切换到其他的控件上面,被调用到。如果返回false,用户将不能切换到其他控件,键盘还会停留在屏幕上
-textFieldDidEndEditing:当一个编辑session结束的时候,调用。当用户决定编辑其他的控件或者dissmiss了键盘
-textField:shouldChangeCharactersInRange:replacementString:当text field里面的text被修改的时候调用, 如果是用户手动输入,每改一个字符都会调用一次,但是用户也可能一个paste很多内容。返回Bool值,如果返回true,表示你容许text被修改,当这个函数被调用时传入的textField,是change之前的值,新的更改是用range和replacementString传入
-textFieldShouldClear:每一个text field都有一个clear按键,当用户按下这个按键,text field的内容会自动清除,我们需要手动enable这个clear按键,如果clear按键enable了,这个函数应该返回true,否则会出现,用户按了这个按键,不起作用的情况
-textFieldShouldReturn:当用户按下return/enter健的时候,想要dismiss键盘。你应该在这个方法里面reassign(释放)这个text field的first responder, 在释放的时候,最好用self.view.endEditing: 这样可以保证释放,如果用reassign,有一些first responder会拒绝resign。

UITextField可以插入view, 用leftView和rightView:

var myTextField: UITextField!
var myLable:UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    myTextField = UITextField(frame: CGRect(x: 38, y: 30, width: 220, height: 31))
    myTextField.delegate = self
    myTextField.borderStyle = .RoundedRect
    myTextField.contentVerticalAlignment = .Center
    myTextField.textAlignment = .Left
    myTextField.placeholder = "Sir Richard Sir Richard"
    myTextField.returnKeyType = .Done
    myTextField.center = view.center
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

let currencyLabel = UILabel(frame: CGRectZero)
currencyLabel.text = NSNumberFormatter().currencySymbol
currencyLabel.font = myTextField.font
currencyLabel.textAlignment = .Right
currencyLabel.sizeToFit()
currencyLabel.frame.size.width += 10
myTextField.leftView = currencyLabel
myTextField.leftViewMode = .Always
view.addSubview(myTextField)
}

注意,如果不设置leftViewMode的话,leftView是不会显示的。如果设置成UITextFieldViewModeWhileEditing, 那么用户编辑的时候才会显示。如果设置成UITextFieldViewModeUnlessEditing,那么用户不编辑的时候才会显示。

1.17 UITextView

希望显示多行text在一个可以滑动的view上的时候,可以选用。

var textView: UITextView!
let defaultContentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)

override func viewDidLoad() {
super.viewDidLoad()
textView = UITextView(frame: view.bounds)
if let theTextView = textView {
theTextView.text = “Some text goes here …”
theTextView.contentInset = defaultContentInset
theTextView.font = UIFont.systemFontOfSize(16)
view.addSubview(theTextView)
}
}

其中contentInset的意思是,文字内容相对于TextView的间隔,defaultContentInset的设置是,文字顶部和TextView顶部的间隔是10 piont,而下面一段代码,表示文字底部和 TextView底部的间隔是一个键盘的长度。

let keyboardRectAsObject = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
var keyboardRect = CGRectZero
keyboardRectAsObject.getValue(&keyboardRect)

    textView.contentInset = UIEdgeInsets(top: defaultContentInset.top, left: 0, bottom: keyboardRect.height, right: 0)
  • 1

附录:
使用键盘,主要是要监听键盘的notification
-UIKeyboardWillShowNotification
-UIKeyboardDidShowNotification
-UIKeyboardWillHideNotification
-UIKeyboardDidHideNotification
监听键盘notification

NSNotificationCenter.defaultCenter().addObserver(self, selector: “handleKeyboardDidShow:”, name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: “handleKeyboardWillHide:”, name: UIKeyboardDidHideNotification, object: nil)

移除键盘notification

NSNotificationCenter.defaultCenter().removeObserver(self)

1.18 UIScrollView

Content size: 是一个CGSize,定义了一个scroll view的content的宽和高。

var imageView: UIImageView!
var scrollView: UIScrollView!
let image = UIImage(named: “Safari”)

override func viewDidLoad() {
    super.viewDidLoad()

    imageView = UIImageView(image: image)
    //scrollview 自身的大小并没有超出屏幕
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.addSubview(imageView)
    //contentSize超出屏幕了
    scrollView.contentSize = imageView.bounds.size
    view.addSubview(scrollView)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

UIScrollViewDelegate:
-scrollViewDidScroll: 当一个scroll view的内容被滚动到的时候,调用
-scrollViewWillBeginDecelerating:用户滑动scroll view的内容,而且手指离开的时候调用
-scrollViewDidEndDecelerating:scroll view停止滑动的时候
-scrollViewDidEndDragging:willDecelerate: 当用户停止drag scroll view内容的时候,注意是drag而不是scroll(滑动),比如:用户放一个手指在scroll view上,移动手指,然后抬起,但是没有给scroll view任何滑动的动力

在移动的时候,会出现一条指示线,如果不想要的话,可以:
theScrollView.indicatorStyle = .White

1.19 WKWebKit (iOS 8 and up)

显示一个web page,并定义其用户交互。需要实现下面的类和协议:
-WKWebView
-WKPreferences: 偏好设定,比如: 不希望load javascript
-WKWebViewConfiguration: 把WKPreferences放到WKWebViewConfiguration里面,然后传给web view
-WKNavigationDelegate:协议,比如:检测当用户点击网页上的link时,时接受还是拒绝请求

var webView:WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    let preferences = WKPreferences()
    preferences.javaScriptEnabled = false

    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences

    webView = WKWebView(frame: view.bounds, configuration:configuration)
    if let theWebView = webView {
        let url = NSURL(string: "http://www.apple.com")
        let urlRequest = NSURLRequest(URL: url!)
        theWebView.loadRequest(urlRequest)
        theWebView.navigationDelegate = self
        view.addSubview(theWebView)
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

其中常用的delegate:
-didStartProvisionalNavigation: 开始一个请求
-didFinishNavigation:结束一个请求
-decidePolicyForNavigationAction: 当一个NavigationAction开始的时候,比如:用户点击了网页中的一个链接,或者退回到前一页
-webView:decidePolicyForNavigationResponse:decisionHandler: 当一个response返回的时候调用,比如:一个link请求的数据回来了,这个函数会被调用,你可以通过这个函数得到请求的数据,接受或者拒绝显示

1.20 UIWebView

初始化方法有:
-loadData:MIMEType:textEncodingName:baseURL: load 一个NSData
-loadHTMLString:baseURL: load一个NSString, 字符串里面需要是有效的HTML,或者一些web browser可以渲染的字符
-loadRequest: load一个NSURLRequest

var frame = view.bounds
frame.origin.y = UIApplication.sharedApplication().statusBarFrame.height
frame.size.height -= frame.origin.y

    let webView = UIWebView(frame: frame)
    webView.delegate = self
    webView.scalesPageToFit = true
    let url = NSURL(string: "http://www.apple.com")
    let request = NSURLRequest(URL: url!)
    webView.loadRequest(request)
    view.addSubview(webView)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

要实现UIWebViewDelegate:
webViewDidStartLoad
webViewDidFinishLoad
didFailLoadWithError

1.21 UIProgressView

UIProgressViewStyle 有2个可选参数:
UIProgressViewStyleDefault:
UIProgressViewStyleBar:可以用作progress view,来添加到toolbar上

override func viewDidLoad() {
super.viewDidLoad()

    let progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Bar)
    progressView.center = view.center
    //值是在+0 到 +1.0
    progressView.progress = 1/2
    progressView.trackTintColor = UIColor.lightGrayColor()
    progressView.tintColor = UIColor.blueColor()
    view.addSubview(progressView)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.22 Provision profile

t.b.d

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/419076
推荐阅读
相关标签
  

闽ICP备14008679号