1

Does anyone know of any tutorials for coding autolayout in Swift? Usually I'd try using an Objective-C one but there is code that doesn't seem to be convertible to Swift. Basically I want a banner which slides in to the view from the bottom of the navigation bar, shrinking down a UITextView which is usually in place. It's astonishing that Apple offers no example of how to integrate an iAD banner correctly in Swift. I've been messing about for two days now and I'm getting nowhere.

EDIT: I am attaching a screen shot because I'm not sure I've explained clearly enough what it is I am trying to do. Notice that when the banner is displayed the UITextView is covered by the iAd banner view. I have absolutely no idea using AutoLayout how to remove the constraint binding the UITextView to the bottom of the Navigation Bar and instead apply a constraint to bind it to the bottom of the iAdBannerView.

enter image description here

KelticKoder
  • 115
  • 2
  • 10
  • Post what you have so far so people can give some guidance. – Acey Dec 15 '14 at 21:30
  • The way to do this using Swift or Objective-C would be fairly similar. Perhaps you should break down your problem into steps so you can determine where it is going wrong. For example, does your project work if you simply try to display the iAd without animation? – ThomasW Dec 15 '14 at 21:39
  • 1
    Assuming you're targeting iOS 7 and above, have you tried simply setting your view controller's `canDisplayBannerAds` property to `true` in `viewDidLoad`? That was all I needed to do to get what you're describing. No need for any other code or any changes in Interface Builder; it Just Worked. – Matt Gibson Dec 15 '14 at 21:50
  • The problem with canDisplayBannerAds is that it only displays them a the bottom and due to my app being most about text entry this gets covered by the keyboard. As to what I have now, I tried a few things based on old Objective-C code and I basically have nothing. Nothing worked even slightly. – KelticKoder Dec 16 '14 at 05:24
  • Might be worthwhile to Checkout https://github.com/Masonry/Snappy. I use Masonry for coding auto layout in objc, Snappy is their (in process) port to Swift. – Joel Bell Dec 17 '14 at 21:44

2 Answers2

1

Its pretty hard to understand exactly what you want without any code but if you add a constraint between the between your text view and the add banner and then when you the ad banner to be displayed just amend the constant value of the constraint. Here are some layouts that could be applied to the textview and iAdview to achieve this behaviour

The constraints for the iadview would be as follows

@"V:[textView]-(0)-[iadView(==44)]"
@"H:|-(0)-[iadView]-(0)-|"

Then if you had the following constraints for the textview

@"V:[textView]-(x)-|"
@"H:|-(0)-[textView]-(0)-|"

Then if you changed the value of x to 44 if would decrease the size of the textview and show the iadView :)

Also if you want this to be animated just called

[self layoutIfNeeded]

DairySeeker
  • 326
  • 3
  • 7
  • I've not even got to the stage now because the AdBanners wont even take the correct sizing. I am getting a portrait AdBanner positioned where it should be for landscape when in portrait mode. I have no idea why Apple are making this so difficult considering it's a revenue generating framework. – KelticKoder Dec 17 '14 at 21:43
1

I don't know exactly why you are stuck, but here are two very good tutorials for AutoLayout :

This one with Swift code : http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1

And this one is more of a generic concept about Size Classes : http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial

If you understand these, you should be able to do what you want.

Mehdi.Sqalli
  • 510
  • 1
  • 5
  • 22
  • I've looked through these but they all relate to using Autolayout with Interface Builder. They don't cover how I would handle removing a UI element completely, or add a new one in programatically. The problem is that if I want to add an iAd banner to my view it breaks Autolayout because the exiting elements can't be rewired to the new object, they still have the exiting constraint and I have no idea how to remove this. – KelticKoder Dec 28 '14 at 06:33
  • Did you check this stackoverflow answer : http://stackoverflow.com/questions/24771295/iad-in-xcode-6-with-swift?rq=1 – Mehdi.Sqalli Dec 28 '14 at 10:31
  • I had not seen it, but unfortunately it's not covering anything I'm trying to do. I have a UITextView with it's top bound to the bottom of my navigation bar. I wish to be able to, on demand, insert an iAd between the navigation bar and the UITextView. Conversely I wish to also be able to remove it on demand. – KelticKoder Dec 28 '14 at 18:57