0

I am trying to present a quote using a presented from the uipresentationcontroller api view but it is not working. What am I doing wrong? also, how do I resize the presented view dynamically to fit the text? Thanks.

this is my code:

override func presentationTransitionWillBegin() {

    presentedView()!.layer.cornerRadius = 15.0

    //adding label for quote to the presented view
    let label = UILabel(frame: CGRectMake(presentedView()!.frame.origin.x, presentedView()!.frame.origin.y, presentedView()!.bounds.width, presentedView()!.bounds.height))
    label.center = presentedView()!.center
    label.textAlignment = NSTextAlignment.Center
    label.text = readQuotesFromLibrary()
    presentedView()?.addSubview(label)
    //rest of the code dealing with uipresentationcontroller goes here ...

As you can see the text is off }

irkinosor
  • 766
  • 12
  • 26

3 Answers3

0

If you are assigning presented view's frame to label,then why need to assign presented view's center to label center.Label will be drawn as presented View's frame.

0

The frame of your UILabel is relative to its superview, which in this case is the presentedView, not the view the presentedView is on top of. Thus, you should instantiate the label with the line:

let label = UILabel(frame: CGRectMake(0, 0, presentedView()!.bounds.width, presentedView()!.bounds.height))

This places the top left corner of the UILabel the top left corner of the presentedView, and gives it the same width and height as the presentedView.

0

I have found making CGRects to give unintended results sometimes, like in your case. If you would like to try an alternative, I would recommend layout constraints. I believe the code below should work for you.

override func presentationTransitionWillBegin() {

    presentedView()!.layer.cornerRadius = 15.0

    //adding label for quote to the presented view
    let label = UILabel()
    label.text = readQuotesFromLibrary()
    label.textAlignment = NSTextAlignment.Center

    presentedView()!.addSubview(label)
    label.translatesAutoresizingMaskIntoConstraints = false
    label.widthAnchor.constraintEqualToAnchor(presentedView()!.widthAnchor).active = true
    label.heightAnchor.constraintEqualToAnchor(presentedView()!.heightAnchor).active = true
    label.centerXAnchor.constraintEqualToAnchor(presentedView()!.centerXAnchor).active = true
    label.centerYAnchor.constraintEqualToAnchor(presentedView()!.centerYAnchor).active = true

    //rest of the code dealing with uipresentationcontroller goes here ...

I wouldn't be surprised if you also run into a text-wrapping issue because the quote in the screen shot won't fit in the presentationView; in that case you may want to use an attributedString and allow the string to span multiple lines. There are various ways to allow a label to span multiple lines; so an attributedString is not the only way to do this.

I hope that helps! Sorry if you really need to go the CGRect way and don't find this useful.

EndersJeesh
  • 427
  • 1
  • 4
  • 20