1

I got some UILabel which can be pressed to open a menu. When pressed, they call my function with the label itself as a parameter. I make a popover that behave correctly in phone view but I would like to position it when it is on ipad view.

My problem is that I can not get the exact position of the UILabel that was pressed.

I tried

myYCoordinates = label.bounds.origin.y

or

myYCoordinates = label.frame.origin.y

to

optionMenu.popoverPresentationController?.sourceRect = CGRectMake(self.view.bounds.size.width / 2, myYCoordinates, 1.0, 1.0)

But I cant get the good coordinate.

I don't know if it is because my label are in a tableview with 2 section or if there is a way at all.

Thank in advance for the help.


Here is my sceen (cropped du to development confidentiality) enter image description here Here is what append when I press my label enter image description here And here is what I would like ti to look like enter image description here


I tried optionMenu.popoverPresentationController?.sourceRect = label.frame

enter image description here

Incognito
  • 493
  • 2
  • 8
  • 22

2 Answers2

2

Based on @Özgür Erzil answer, I found out that I could simply use

optionMenu.popoverPresentationController?.sourceView = label
optionMenu.popoverPresentationController?.sourceRect = CGRectMake(label.frame.width, label.frame.height / 2, 1.0, 1.0)

By setting the source view to my label and using the label width and height to position it.

Thank

Incognito
  • 493
  • 2
  • 8
  • 22
-1

if you create your UILabel dynamically, try to set the position when you set it like: CGRectMake(x, y, width, height)

var label = UILabel(frame: CGRectMake(0, 0, 200, 20))

or if it is in storyBoard, you have to use constraint methods and use autolayout. Explained here

Community
  • 1
  • 1
Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
  • My label are created in the storyboard, I do not make them dynamically. There position on screen will also vary depending on the option I show / hide on screen. They are in my second section of my tableview. – Incognito Jun 02 '15 at 16:25
  • What you linked was to vertically center a text in a UILabel but what I want to center is a popover added to the view... I will add an image of what I want to do in some minutes. – Incognito Jun 02 '15 at 16:32
  • I edited my post with image of what i'm trying to do – Incognito Jun 02 '15 at 16:51
  • try this: popoverPresentationViewController?.sourceRect = yourTextField.frame – Özgür Ersil Jun 02 '15 at 16:56
  • It almost does what I want, except that it is not aligned vertically with the label.... (check the update to see the result) – Incognito Jun 02 '15 at 17:03
  • it is because your textfield position is probably 0, add the textview superview position too in this case and be sure if textfield is the selected one – Özgür Ersil Jun 02 '15 at 17:09
  • I'm a little nood in swift, hw can I use the superview with the sourceRect? – Incognito Jun 02 '15 at 17:14
  • try to reach the selected view with a parameter sender.In this case it will be a label. You can get it optionMenu.popoverPresentationController. sourceRect = sender.frame – Özgür Ersil Jun 02 '15 at 17:21
  • I forgot to set the sourceView to my label, it is more easy like that, I posted the answer to be clear or would you prefer to edit your answer so that I can give you the green check answer ? – Incognito Jun 02 '15 at 17:23
  • great that you did it, happy coding! – Özgür Ersil Jun 02 '15 at 17:24