How to Make a Popup Window with an Image Swift

How to make a popup window with an image SWIFT

I would simply create a reusable UIView component and everything you need as a subview, such as a UIImageView for your image, a UILabel or a UIButton in the top right. Here is the process to show it:

  1. Create a UIView that takes up the full screen, make it black, and maybe 0.5 alpha.
  2. Create another UIView which is your primary pop-up view, make it slightly smaller than the previous view, but make sure both of these views are subviews of the parent subview.
  3. Add the desired elements on to the pop-up view as subviews, I would even suggest creating a UIView subclass if you plan to use this a lot.
  4. To present the pop-up, make sure both views are set to hidden = true when created and so that when a button is selected, you can set them to hidden = false
  5. If you would like them to be animated, simply start them off with alpha = 0.0 and use something like UIView's animateWithDuration and set the pop-up view to alpha = 1.0

There is a lot of little details you can change to cater to your needs, but this is the basic structure on how to accomplish your goal.

Check out UIView animation methods here.

How to create a custom pop up view with swift?

you'd create an custom UIView with all respected object needed, from your Controller's viewDidLoad() you'll hide it.

customView.hidden = true

Whenever your user wants to perform some action or task, you unhide it and once the user finished then hide it again or remove from the superView.

customView.hidden = false

Below there is some code to help you start

    private var customView: UIView!

override func viewDidLoad() {
super.viewDidLoad()
customView.hidden = true
}

private func loadCustomViewIntoController() {
let customViewFrame = CGRect(x: 0, y: 0, witdh: view.frame.width, height: view.frame.height - 200)
customView = UIView(frame: customViewFrame)

view.addSubview(customView)

customView.hidden = false

// any other objects should be tied to this view as superView
// for example adding this okayButton

let okayButtonFrame = CGRect(x: 40, y: 100, width: 50, height: 50)
let okayButton = UIButton(frame: okayButtonFrame )

// here we are adding the button its superView
customView.addSubview(okayButton)

okayButton.addTarget(self, action: #selector(self.didPressButtonFromCustomView:), forControlEvents:.TouchUpInside)

}

func didPressButtonFromCustomView(sender:UIButton) {
// do whatever you want
// make view disappears again, or remove from its superview
}

@IBAction func rateButton(sender:UIBarButtonItem) {
// this barButton is located at the top of your tableview navigation bar
// when it pressed make sure you remove any other activities that were on the screen, for example dismiss a keyboard

loadCustomViewIntoController()
}

Check it out this github project, It's closed to be production ready, it gives you a better way to deal (present and dismiss) with UIViews

If you only want the player's name then use a UIAlertController containing a textfield

How to open popup window contain UIview or image

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Popup Title" 
message:@"This is pop up window/ Alert"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

UIImageView *tempImageView=[[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)];

tempImageView.image=[UIImage imageNamed:@"abcd.png"];

[alert addSubview:tempImageView];

[alert show];

Popup window in swift for tutorial view

If i want a stylised popup then I will create a new Nib file and design it in there. Then when you want to show the popup you can load that NIB and animate it in, upon closing it you can animate it out.

Popupwindow with image

Create custom-dialog and pass image in it....

private void loadPhoto(ImageView imageView, int width, int height) {

ImageView tempImageView = imageView;

AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

View layout = inflater.inflate(R.layout.custom_fullimage_dialog,
(ViewGroup) findViewById(R.id.layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
image.setImageDrawable(tempImageView.getDrawable());
imageDialog.setView(layout);
imageDialog.setPositiveButton(resources.getString(R.string.ok_button), new DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}

});

imageDialog.create();
imageDialog.show();
}

custom_fullimage_dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="@+id/fullimage" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageView>

<TextView android:id="@+id/custom_fullimage_placename"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:textColor="#FFF">
</TextView>
</LinearLayout>


Related Topics



Leave a reply



Submit