简体   繁体   English

iPhone:如何在iPhone中显示像控制器一样的弹出视图?

[英]iPhone : How to display popup view like controller with image in iPhone?

I want to display small popup view (just like popup view in iPad but here I want in iPhone) in iPhone with image. 我希望在带图像的iPhone中显示小弹出视图(就像iPad中的弹出视图,但我想在iPhone中)。

How can I do that ? 我怎样才能做到这一点 ?

@devang you would certainly appreciate this http://iosdevelopertips.com/open-source/ios-open-source-popover-api-for-iphone-wepopover.html @devang你当然会欣赏这个http://iosdevelopertips.com/open-source/ios-open-source-popover-api-for-iphone-wepopover.html

The other approach is what Mehul suggested. 另一种方法是Mehul建议的。 Do let us know if you come across something which corresponds to UIPopover in iPad. 如果您遇到与iPad中的UIPopover相对应的内容,请告诉我们。

You can take UIView dynamically and then add UIImageView in this UIView just like this 你可以动态地使用UIView,然后像这样在这个UIView中添加UIImageView

    UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(XPosition, YPosition, Width, Height)];       
    UIImage *tmpImg = [UIImage imageNamed:@"YourImageName.png"];
    UIImageView *tmpImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tmpImg.size.width, tmpImg.size.height)];
    tmpImgView.image = tmpImg;
    [self.view addSubview:tmpView];

Hope this will Work.... 希望这会起作用....

Collectionview Can be popUp on screen. Collectionview可以在屏幕上弹出。 Below is complete code for showing collectionview as popup and hiding the popup window 下面是将collectionview显示为弹出窗口并隐藏弹出窗口的完整代码

import UIKit

class ViewController: UIViewController , UICollectionViewDataSource, UICollectionViewDelegate  {

    //let popView = UICollectionView.init(frame: CGRect(x:8,y:30,width:304,height:200))

    var demoCollectionView:UICollectionView? = nil;

    @IBOutlet var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.loadColorCollectionView()

    }

    func loadColorCollectionView() {
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 30, height: 30)

        demoCollectionView = UICollectionView(frame: CGRect(x:8,y:20,width:304,height:200), collectionViewLayout: layout)


        demoCollectionView?.dataSource = self
        demoCollectionView?.delegate = self
        demoCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        demoCollectionView?.backgroundColor = UIColor.white
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1;
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 200;
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath);

        if indexPath.row%2 == 0 {
        cell.backgroundColor = UIColor.red;
        }
        else{
            cell.backgroundColor = UIColor.blue;
        }
        return cell;
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }

        if indexPath.row%2 == 0 {
            statusBar.backgroundColor = UIColor.red;
        }
        else{
            statusBar.backgroundColor = UIColor.blue;
        }
        self.demoCollectionView?.removeFromSuperview()

    }

    @IBAction func showMessage() {

        //popView.backgroundColor = UIColor.darkGray;
        //self.view.addSubview(popView)


        self.view.addSubview(demoCollectionView!)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //self.popView.removeFromSuperview()
        self.demoCollectionView?.removeFromSuperview()


    }

}

I created a button in Viewcontroller and called showMessage() function on button click it will pop up a collectionview with colors. 我在Viewcontroller中创建了一个按钮,并在按钮上调用了showMessage()函数,它会弹出一个带颜色的集合视图。 On select of color popup will disapear. 选择颜色弹出将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM