【问题标题】:iPhone : How to display popup view like controller with image in iPhone?iPhone:如何在 iPhone 中显示像控制器一样的弹出视图?
【发布时间】:2011-05-21 12:25:01
【问题描述】:

我想在 iPhone 中显示带有图像的小弹出视图(就像 iPad 中的弹出视图,但我希望在 iPhone 中显示)。

我该怎么做?

【问题讨论】:

标签: iphone objective-c cocoa-touch ios4


【解决方案1】:

@devang 你一定会喜欢这个http://iosdevelopertips.com/open-source/ios-open-source-popover-api-for-iphone-wepopover.html

另一种方法是 Mehul 建议的。如果您在 iPad 中遇到与 UIPopover 对应的内容,请告诉我们。

【讨论】:

  • 不错的链接。但它只允许文本而不是图像。我想显示图像。
【解决方案2】:

你可以动态取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];

希望这会奏效....

【讨论】:

  • 但它不会产生与 iPad 中 Popoverview 相同的效果。我想要在 iPhone 上同样的效果!
【解决方案3】:

Collectionview 可以在屏幕上弹出。下面是将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()


    }

}

我在 Viewcontroller 中创建了一个按钮,并在单击按钮时调用了 showMessage() 函数,它将弹出一个带有颜色的 collectionview。选择颜色弹出框将消失。

【讨论】:

    猜你喜欢
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多