PopoverController for iPhone

来源:互联网 发布:中国移动宽带拨号软件 编辑:程序博客网 时间:2024/06/06 10:43

There is no build-in popup windows or popover controllers for iPhones. Only iPad can use popover controller class.

So i use a third-party controller called FPPopover from github. https://github.com/50pixels/FPPopover


But it is not well-developed. There is a bug in FPPopoverView.m, which leads to the blue label when the arrow is at Up direction and the tint is set to white. It should be:

elseif(self.tint ==FPPopoverWhiteTint)

    {

        colors[0] = colors[1] = colors[2] =1.0;

        colors[4] = colors[5] = colors[6] =1.0;

        colors[3] = colors[7] =1.0;

    }

And there are no properties for arrow height, rounded radius. Users need to modify them in FPPopoverView.m if they don't want rounded rect.


How to combine OC and Swift?

Create a popover-Bridging-Header.h file

#Import "FPPopoverController.h" of the third-party framework

Add the relative path of popover-Bridging-Header.h, which is: popover/popover-Bridging-Header.h

Then you can use it with Swift


How to use it?

Add files (mentioned in the website), then add delegate to your VC

    var popoverView: FPPopoverController?


var contentVC =PopupViewController()

        popoverView =FPPopoverController(viewController: contentVC)

        popoverView!.contentSize =CGSizeMake(160,215)

        popoverView!.tint =FPPopoverWhiteTint

        popoverView!.arrowDirection =FPPopoverNoArrow

        popoverView!.border =false

        popoverView!.setShadowsHidden(true)

        popoverView!.delegate =self


        popoverView!.presentPopoverFromPoint(CGPoint(x: 0, y: _naviHeight))


func popoverControllerDidDismissPopover(popoverController:FPPopoverController!) {}


class PopupViewController:UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {


    var mthPicker:UIPickerView?

    var mthPickerDSyear:[Int] = []

    var bgWidth  =UIScreen.mainScreen().bounds.size.width

    var bgHeight =UIScreen.mainScreen().bounds.size.height


    overridefunc viewDidLoad() {

        super.viewDidLoad()

        for iin 0...99 {

            mthPickerDSyear.append(selectedYear! -99 + i)

        }

        mthPicker =UIPickerView(frame: CGRect(x:0, y: -20, width:150, height: bgHeight))

        mthPicker!.dataSource =self

        mthPicker!.delegate   =self

        mthPicker?.backgroundColor =UIColor.whiteColor()

        mthPicker!.selectRow(mthPickerDSyear.count - 1, inComponent: 0, animated:false)

        mthPicker!.selectRow(selectedMonth! -1, inComponent: 1, animated:false)


        self.view.addSubview(mthPicker!)


    }

    

    //UIPickerView

    func numberOfComponentsInPickerView(pickerView:UIPickerView) -> Int {

        return2

    }

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

        if (component ==0){

            returnmthPickerDSyear.count

        }else{

            return12

        }

    }

    func pickerView(pickerView:UIPickerView, titleForRow row: Int, forComponent component: Int) ->String! {

        if (component ==0){

            return"\(mthPickerDSyear[row])"

        }else{

            return"\(row +1)"

        }

    }

    func pickerView(pickerView:UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if (component ==0){

            selectedYear  =mthPickerDSyear[row]

        }else{

            selectedMonth = row +1

        }

    }


    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */


}


0 0
原创粉丝点击