0

I have a UIImageView, and a series of conflicting constraints set on it.

I set the active property of some of them to true or to false at different times, and it works—the image view is always where it's supposed to be.

However, in another method, I use it's frame to calculate the position of another view, and so I noticed that it's frame isn't where the image view appears. For example, the image view appears centered in the middle of the screen, but it's frame (I created another UIView and set it's frame to the image view's frame to test this), is in the bottom left—where the image view used to be before changing which constraints were active.

Is there a way I can update the frame so that it reflects where the image view actually is? Or, is there a way to get the true frame of the image view?

Here's the code (qrCode is the view I'm trying to arrange, myContainer is its superview):

self.qrCode.setTranslatesAutoresizingMaskIntoConstraints(false)

//this sets qrCode 0 pts from the bottom of its parent
self.bottom.active = false

//this centers qrCode vertically in its parent
self.center.active = true 

//these set how far the parent is from the edges of its view
self.newLeft.active = true
self.newRight.active = true

let testView = UIView(frame: qrCode.frame)
testView.backgroundColor = UIColor.greenColor()
self.myContainer.addSubview(testView)

All this code positions the image view (qrCode) correctly, but it's frame (as shown by testView is in the wrong location—it's where qrCode was before the constraints were configured.

Randoms
  • 2,110
  • 2
  • 20
  • 31
  • are you using storyboards or raw code? – Larry Pickles Aug 27 '15 at 04:23
  • @Larcerax Both—the original image view and constraints were made in storyboard, but I made outlets for the constraints which I use to change the `active` property. – Randoms Aug 27 '15 at 04:25
  • are you animating the constraints using the constraint constant or some other way or if you aren't animating them at all, are you at least using the constants to change the positin of the UIImageView? The constants as in this: NSLayoutConstraint(item: labelMessage, attribute: .Top, relatedBy: .Equal, toItem: contentView, attribute: .Top, multiplier: 1.0, constant: 35) ... the constant is the 35 – Larry Pickles Aug 27 '15 at 04:29
  • I'm not animating the constraints at all, and I'm not changing the constant. The only thing I'm doing is setting the `active` property of some to to true, and the `active` property of others to false. – Randoms Aug 27 '15 at 04:30
  • Ahh k, are you using "setTranslatesAutoResizginMaskIntoConstraints = FALSE" – Larry Pickles Aug 27 '15 at 04:31
  • No, I'm not calling that method at all. – Randoms Aug 27 '15 at 04:32
  • k, so you may have a couple of issues to look at. First off, the contentMode of the picture in the imageveiw, this is going to be the least of your worries, but still something to check, if the contentmode is set to smoething where the picture looks like its represenative of the UIIMageView frame then you need to unset contentmode and check where the image ends up. the next thing to look at is this, if you are using constraints as in NSLayoutConstraints then tthey won't work unless you set the property I asked you above in the previous commment – Larry Pickles Aug 27 '15 at 04:34
  • if you use NSLayoutConstratins without first setting setTranslatesAutoResizginMaskIntoConstraints = false then your constratins wont do anything, so, if you HAVE NOT given your imageview a frame in your code as in you have not done anything like "imageview.frame = CGRectMake( blh blah balah.." AND yuo haven't setTranslatesAutoResizginMaskIntoConstraints to false then you don't have the frame you want – Larry Pickles Aug 27 '15 at 04:35
  • You can only have one or the other, so this is the tradeoff, you either SET the frame using something like CGRect and forget about the NSLayoutConstraints or you set this "setTranslatesAutoResizginMaskIntoConstraints = false" and then use raw NSLayoutConstraints – Larry Pickles Aug 27 '15 at 04:36
  • Once you pick which one you want, then it's easy from there, here's some constraints prefabbed for you: https://gist.github.com/anonymous/2a9a8c8731154b61fbc1 – Larry Pickles Aug 27 '15 at 04:38
  • I don't think the `contentMode` is the issues as setting the `image` to nil and giving the image view a background color shows it in the same place. Not quite sure what you're saying. I never set the frame of the image view programmatically. – Randoms Aug 27 '15 at 04:41
  • If you have questions, just ask, but I won't be posting answers in the answer box because the midnight nerd crew is live right now and they are going to hit a downvoting storm for no reason like last night so I dont want to chance this. I can post an answer or a preview tomorrw morninng if you want it, but ask as many questions as you want to – Larry Pickles Aug 27 '15 at 04:41
  • Doing `setTranslatesAutoresizingMaskIntoConstraints(false)` does nothing—the image view is still in the right place as defined by the constraints but its frame is in the wrong place. Doing `setTranslatesAutoresizingMaskIntoConstraints(true)` moves the image view to where its frame is (where I don't want it to be). – Randoms Aug 27 '15 at 04:42
  • No no, I'm just telling you what to look for, if you dont' have the content mode set then you are okay, the next thing is that if YOU DON'T set a frame AND you are using constraints, then you MUST do this "imageview.setTranslatesAutoResizginMaskIntoConstraints = false" – Larry Pickles Aug 27 '15 at 04:42
  • Yeah, so, I'm glad you tried this, you'll have to post code and we can go from there, there's some sort of disconnect that's happening between the code and the constraints – Larry Pickles Aug 27 '15 at 04:43
  • Ah, I see. Well then if I do `setTranslatesAutoresizingMaskIntoConstraints(false)`, since that moves my image view to the wrong location, how can I move it back where I want to be? Would I have to set its frame programmatically. (Feel free to post an answer whenever you think is best). – Randoms Aug 27 '15 at 04:43
  • Yes!! you are getting it, and I'm not being sarcastic, it took me like 5 times thinking through this when i first started, that's great! So, you have to set the constraints to "wrap the imageview" into a constratined frame, I'll post an example of centering a uiimageview one sec – Larry Pickles Aug 27 '15 at 04:45
  • Great. Thanks so much. Check out the code I'm using above. – Randoms Aug 27 '15 at 04:49
  • here you go: https://gist.github.com/anonymous/c17880761b394275c3c1 – Larry Pickles Aug 27 '15 at 04:49
  • oh perfect, let me look at that then, the GIST i just posted is show a frame of 100 width and 100 height using constraints, don't worry about the origin x and origin y, they are set automatically by the system and then after the "ayoutsubeivews" methods are performed they will be set, but DON"T using the the "layoutsubeivews" methods yourself, you just place this code in like your viewDidLoad in your view controler and your are good to go – Larry Pickles Aug 27 '15 at 04:51
  • Also, what is this "self.bottom.active = false" which library are you using that gives you this "active" command? – Larry Pickles Aug 27 '15 at 04:53
  • Awesome, by the way, in your code, is labelMessage supposed to be the UIImageView, `a`? – Randoms Aug 27 '15 at 04:53
  • The `active` property is built in by Apple to `NSLayoutConstraint`. Take a look under "Activating and Deactivating Constraints": https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/ – Randoms Aug 27 '15 at 04:54
  • yesh, sorry about that, yep, just change the variable names – Larry Pickles Aug 27 '15 at 04:54
  • ahh those are constraints, i see, nevermind, yeah, I thought that was the view you were you doing that to – Larry Pickles Aug 27 '15 at 04:54
  • No problem. BTW, when I change all the uses of `labelMessage` to `a` I get some weird errors. `Cannot invoke addConstraint with argument list of type (NSLayoutConstraint)`, `Could not find member '.Top'`, `Could not find member '.CenterX'`. – Randoms Aug 27 '15 at 04:57
  • I see, one sec, I don't use constratins like you are doing, EVER, since it's easier for me to set the whole long line and mess with them way I showed you, one sec – Larry Pickles Aug 27 '15 at 04:57
  • Ah, ok. Let me know if it would help if I posted screenshots of the details of those constraints. – Randoms Aug 27 '15 at 04:58
  • wow, that was dumb of me, hold up, I'm reposting – Larry Pickles Aug 27 '15 at 04:59
  • No prob. Take your time. I've got an idea: I wonder if instead of creating the `UIView` whose frame is dependent upon the frame of the original image view programmatically, if I could set constraints for _that_ in storyboard and they would be adjusted correctly. – Randoms Aug 27 '15 at 05:00
  • this is it! and I made sure it compiles this time! https://gist.github.com/anonymous/00bdeac82af1acb3cecb – Larry Pickles Aug 27 '15 at 05:01
  • You might be able to set the constrtaints in storybaord like that, I wouldn't know, I've never used storyboards, I only do programmatically – Larry Pickles Aug 27 '15 at 05:02
  • Oh crap, change the "contentView" to "self.view" or whatever the superview is, I'm doing these examples in a UITableVeiwCell – Larry Pickles Aug 27 '15 at 05:04
  • Thanks for all your help! When I run your code (with `let contentView = self.view`), the red view (`a`), takes up the entire screen, the entire `contentView`. Anyway, what do you recommend? If I just bypassed setting the new `UIView` (in my own app) relative to the frame of the original image view and instead used constraints (created programmatically), to position it, would it work? That is, would the `UIView` be positioned relative to the correct position of the image view (where the image view appears to be, not where its frame is. – Randoms Aug 27 '15 at 05:09
  • lol, you knwo how dumb I am, this is how dumb I am, one more try, i sware to god – Larry Pickles Aug 27 '15 at 05:09
  • dont' worry Randoms, I'm posting the REAL solution this time, I made the view the hieght and width of the superview like an idiot – Larry Pickles Aug 27 '15 at 05:10
  • Don't worry! You've been so nice and helpful—I promise to never tell anyone! – Randoms Aug 27 '15 at 05:10
  • thanks LOL, this is the final and definately working, https://gist.github.com/anonymous/71b6978888fb83db8252 – Larry Pickles Aug 27 '15 at 05:12
  • you'll notice that that these use NotAnAttribute fot the attribute with nil as the item related to like this: "toItem: nil" if you've not seen this before, this is how you set the width and height of somethign in constraints. – Larry Pickles Aug 27 '15 at 05:13
  • Works perfectly! And good to know about the `NotAnAttribute`! – Randoms Aug 27 '15 at 05:15
  • Would it be simpler to instead of all this just set programmatic constraints like the ones your gist for the new view? All I want is for it to be 4pts in every dimension larger than the original image view. – Randoms Aug 27 '15 at 05:16
  • IF YOU have any other problems, just ask away – Larry Pickles Aug 27 '15 at 05:16
  • Yep, so I would do it all programmatically, it gives you a little more control, the only problem is that setting it this way, the constratins aren't going to react to different iphone sizes. so when I set the height and width like that in a constraint like I've done, it's usually NOT going to reflect a change in size – Larry Pickles Aug 27 '15 at 05:17
  • OH yeah and YES, if you just need four points bigger, then it's soo much easier to do it this way, and 4 points in all iphone sizes isn't going to be a big deal, you can live with that, no reasson to resize 4 points for different iphone sizes – Larry Pickles Aug 27 '15 at 05:18
  • Ok, thanks for the tip, but I don't think that will be a problem. If I use programmatic constraints though, will they be set relative to the position where the image view appear? Or where its frame is. – Randoms Aug 27 '15 at 05:18
  • And I hate to be lazy, but since you're such a programmatic constraint whiz, would you mind pointing me in the right direction with writing the constraints? – Randoms Aug 27 '15 at 05:21
  • Good question, you shoudl only set the constratins on the immediate parent of the view you are tryign to resize, so if the other view is a subview of "self.view" then just use the same constratins but just add 4 points in all directions with the center constratins the same, so just add 8 points to the width and height constratints. so now you have the imageview and view with two different sets of constrastins in the "self.view" but if you have the view as a subview of the imageview or the other way around then use "imageview.addConstraints" or "view.addConstraints" – Larry Pickles Aug 27 '15 at 05:21
  • Yep, no worries Random, actually, if you tell me how this is set up with the subviews, I'll just toss in the other view constraints for you, is this s subveiw of self.view or is the UIView a subview of the UIImageVeiw or the UIIMageView a subview of the UIView? – Larry Pickles Aug 27 '15 at 05:23
  • YES! Larcerax, you're the best. The image view is a subview of a container view, `myContainer` which holds it and a few other things. The new UIView, if possible, should also be a subview of that. – Randoms Aug 27 '15 at 05:25
  • Okay, so here are three methods of autolayout constraints, using your set up, one uses subviews of the others view, the other two are all added to self.view, you can tesst them all by uncommenting and then recommenting them in viewDidLoad: https://gist.github.com/anonymous/fcd4146b12797e16468c – Larry Pickles Aug 27 '15 at 05:47
  • there's still another 5-10 ways to use constraints like this, but these are going to show you the basics of what it means to program stuff "programmatically" this is how you wold do it – Larry Pickles Aug 27 '15 at 05:47
  • So, I've got this: https://gist.github.com/anonymous/3b954d828a9f380b06d5 – Randoms Aug 27 '15 at 05:59
  • And I see that I can change the size of `b` by setting the constant in the last two constraints, but how can I set it so that the height and width are 8 pts larger than that of a? – Randoms Aug 27 '15 at 06:00
  • Since a's frame doesn't match up with where it is. – Randoms Aug 27 '15 at 06:01
  • okay, show the command where you add this to the view, just in case we need this, but I'm assuming that this is done in storbybaords so we are fine and the view is being recognized by the self.view, that's what I'm hoping – Larry Pickles Aug 27 '15 at 06:02
  • The command where I add which to the view—the image view or the view? The image view I added in the storyboard, and the view I added with line 5 of the gist: `self.myContainer.addSubview(b)` – Randoms Aug 27 '15 at 06:05
  • So, the "qrCode", assumign you can decalre constraints on storyboard views, do this: https://gist.github.com/anonymous/8bf9a89accb41c41a3c1 – Larry Pickles Aug 27 '15 at 06:08
  • this should do it, if they are suppose to be part of the "containerView" otherwise, if this still isn't working, then I would make sure your Containerview has a frame first and/or add the qrcode view to the containerView programmatically – Larry Pickles Aug 27 '15 at 06:09
  • and you'll notice, I just made the "b" frame 8 points larger – Larry Pickles Aug 27 '15 at 06:09
  • Got it. So then, there's no way to just set the edges of the view to go exceed that of the image view by 4 pts? That way, the image view could fall wherever it does (without setting it's height and width to 100), since I've set some constraints for it in the storyboard I like its position to be determined by. – Randoms Aug 27 '15 at 06:14
  • Yeah, you can, but this gets complicated, because the B view doesn't have a frame until the view controller calls the "layoutsubviews" methods, and it usually has to call it two times. So, if you set the frame for the uiimageview to the uiview's frame in the, let's try "willLayoutSubviews" OR the "viewDidAppear" then the frame for B should be set by autolayout ALMOST FOR SURE. so, instead of setting the frame for B, , but with how you are explaining it now, just do this in your ViewDidAppear, one sec – Larry Pickles Aug 27 '15 at 06:17
  • do this in viewDidAppear, it should work and DON"T declare setTranslatesAutoResizingMaskIntoConstraints, just remove this and remove all constraints, and do this: https://gist.github.com/anonymous/2cd20857d0acd4611353 that's it – Larry Pickles Aug 27 '15 at 06:21
  • this will work if what you are telling me is right about the UIImageView frame, just don't do anything until the viewDidAppear in your UIViewControlller and then set the view, this is going to possibly lag and may show a hiccup, but it shouldn't – Larry Pickles Aug 27 '15 at 06:22
  • and this will resize the view to +4 points on all sides relative to the UIImageview – Larry Pickles Aug 27 '15 at 06:23
  • BUT DON"T do this b.frame = qrCode.frame, this isn't going to do it for you, most likely at least – Larry Pickles Aug 27 '15 at 06:25
  • I don't want to put this in viewDidAppear() because this whole layout paradigm only happens if a certain function gets called by the action of a nav bar item. So, the image view won't even necessarily be on the screen at viewDidAppear(). – Randoms Aug 27 '15 at 06:27
  • Is there a way to do something like `self.myContainer.addConstraint(NSLayoutConstraint(item: b, attribute: .Trailing, relatedBy: .GreaterThanOrEqual, toItem: qrCode., attribute: .Trailing, multiplier: 1.0, constant: 4)`)? – Randoms Aug 27 '15 at 06:28
  • Ahh yea, i see then, this spells some trouble, would need to see what you mean, to get the gist of it from this point, otherwise, you need a reference point for the UIImageView to send to the UIView and it looks like there's ZERO frame reference with how you set it up for the UIView to work, – Larry Pickles Aug 27 '15 at 06:28
  • Yes, you can try that constraint, i woudl try it, the only problem is that you don't know when the FRAME value of the UIiamgeView is set so that the value trickels to the constraint, but try it, it's worth a shot – Larry Pickles Aug 27 '15 at 06:29
  • big question, what's the purpose for the UIView, perhaps this is solvable using the layer of the UIImageView itself – Larry Pickles Aug 27 '15 at 06:31
  • So, the image view holds a QR Code, and I want it to have a border. I can't just the related properties of the image view's layer, though, because the border is drawn inside the bounds of the image view and thus cuts off some of the QR code. I also can't resize the QR Code to a bigger image, then draw the border inside, because the border is still drawn over part of the QR code. If there's a simpler way to do this, by all means, let me know. – Randoms Aug 27 '15 at 06:33
  • Yes, i see the dilemna now, Randoms, one sec – Larry Pickles Aug 27 '15 at 06:40
  • Go ahead. I've also tried setting a shadow, as was suggested by someone here: http://stackoverflow.com/questions/15184133/add-a-border-outside-of-a-uiview-instead-of-inside, but that's faded and not solid. Let me know what you think. I may fall asleep soon, but I'll check this first thing tomorrow morning. Thanks!! Also, please feel free to post an answer, and I'll be sure to accept and upvote it of course. – Randoms Aug 27 '15 at 06:40
  • lol, I may fall asleep too, but don't worry, I love answering quesitons, go to sleep if you want to, I'll have something because I love this puzzle you have here – Larry Pickles Aug 27 '15 at 06:41
  • you know what, the easiet way to do this, and I can't believe I didn't tell you this one, is to subclass a UIViewImageView with a UIImageView inside of it, then call this in your story board, and set the class type of the qrCode to thsi custom UIImageView – Larry Pickles Aug 27 '15 at 06:44
  • this will reitain all the properties of UIIMageView and then you can easily set up the border inside this subclass without touching a thing, I do this all the time, in fact this is how programmatic coding works, you subclass everything – Larry Pickles Aug 27 '15 at 06:45
  • one sec, I'm making this for you, almost done – Larry Pickles Aug 27 '15 at 06:49
  • @Larcerax Hi! Did it work? – Randoms Aug 27 '15 at 14:12
  • Just woke up, so here's what we got, I got a subclass moving where it has a uiimageview inside a uiview it's going to be hard to explain how this wroks but the the idea is that it works just like the uiimageview you have works, but instead of setting the imaage like this: qrCode.image = "some image" it will set it like this qrCode.internalImageView.image = "some image" if you can handle this and the fact that the internal image view is always 4 points smaller on each side whiel sitting inside the UIview then we have a winner, the only problem is this – Larry Pickles Aug 27 '15 at 17:44
  • Sounds awesome. That's not a problem at all. Can I still use set constraints storyboard for it? – Randoms Aug 27 '15 at 17:45
  • that you have to set the uiimageview that's inside the uiview to 4 points less per side which means that you'l have to make your adjustments OUTSIDE account for this 4 point difference and thereby messing up your resizing ust a little bit. I tried everything from overriding drawrect to things that souldnt be possible and this is the only thing that seemed viable – Larry Pickles Aug 27 '15 at 17:45
  • yep, with a custom ui subclass you can use it in storyboard – Larry Pickles Aug 27 '15 at 17:46
  • give me liek 30 minutes to wrap it up, i started veering of into some intense c code bridges to override ALL draw functions, so let me back it up and sent it to you – Larry Pickles Aug 27 '15 at 17:46
  • Sounds good. I'll wait. Basically, though, what you're saying is that when I set constraints for the custom class, I'm really setting constraints on the outer view, so I have to account for the 4 point discrepancy? – Randoms Aug 27 '15 at 17:48
  • yep and with this method there isn't any setting of constratins, you just substitute this class in the storyboard with the uiiimagevie class you are using now for the qrCode, but just increase the size of the THIS class by 4 points on all sides and then imageview inside will resize to the exact size you want it – Larry Pickles Aug 27 '15 at 17:57
  • I'm not using any internal constratins either, they aren't necessary for what you want, you just tell storybard that you are using "customclasseXYZ" and it will accept this just fine, but tell me if this is bad news or good news, becaause of the 4 points higher, but you don't need to do anything else but just substitute this class for the current uiimageview you have in storyboard and then increase it's size by 4 points on all sides and you are done. – Larry Pickles Aug 27 '15 at 17:59
  • Here's the subclass, https://gist.github.com/anonymous/5b7102d77094508:9fbd in your viewcontroller, if yoyu want to test it, here's how you wold set it: https://gist.github.com/anonymous/64881449173a0e6843a1 .. in storyboard, it will work just like any old UIView, but you'll want to change the colors inside the subclass, not in storyboard, and for the constratins, just set it as you normally would in storyboard, everythign will flow through – Larry Pickles Aug 27 '15 at 18:04
  • yeah, and I mean this sucks sort of because it's NOT exactly like you want it, but it's like THE only way to do something like this given that you have some very strict rules in place for how you want to do this, also, the imageview inside this uiview is just a normal uiimagview, so treat it as such but only set the frame of the subclass – Larry Pickles Aug 27 '15 at 18:13
  • also, this IS NOT like a border, even though it is a border. I say this because the way it works is like this: the border will clip your UIImageView, this doesn't clip anything, the UIImagView inside this view is it's own view and won't be clipped by it's own layer – Larry Pickles Aug 27 '15 at 18:18
  • Alright then. I'll give it a shot right now and let you know how it goes . – Randoms Aug 27 '15 at 18:55
  • Hm. Not sure if this is a problem on my end, but I can't open the gist containing the subclass code. – Randoms Aug 27 '15 at 19:05
  • weird it keeps being deleted, try this https://gist.github.com/anonymous/d9b6ade935f4f98ee42d – Larry Pickles Aug 27 '15 at 19:08
  • and this https://gist.github.com/anonymous/4b406f9ca959132b6850 – Larry Pickles Aug 27 '15 at 19:09
  • Just kidding! I put in didReceiveMemoryWarning() accidentally. – Randoms Aug 27 '15 at 19:15
  • oh yeah, and the class "QQCustomUIViewForQRImageView" shoudl be imported as a swift file, this is a separate class entirely – Larry Pickles Aug 27 '15 at 19:18
  • miported as "QQCustomUIViewForQRImageView.swift" – Larry Pickles Aug 27 '15 at 19:18
  • Ok, so I imported into its own file, but then when I create a uiview in storyboard, set constraints for it, and made an outlet, I got an error "This class does not support NSCoding". This is what I'm doing: https://gist.github.com/anonymous/01275c796f6b96d8b85c – Randoms Aug 27 '15 at 19:24
  • I forgot to use nscoding, place this inside the subclass Swift file, well i mean replace the entry for this: "required init(coder aDecoder: NSCoder) {" with this: required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) super.layoutSubviews() qrImageView.frame = CGRectMake(4, 4, frame.size.width - 8 , frame.size.height - 8) qrImageView.backgroundColor = UIColor.yellowColor() self.addSubview(qrImageView) fatalError("This class does not support NSCoding") } – Larry Pickles Aug 27 '15 at 19:32
  • https://gist.github.com/anonymous/7f37d45f954f34c67117 – Larry Pickles Aug 27 '15 at 19:32
  • blah, what am I saying, hold up, let me verify something, I gave you the wrong code, one sec – Larry Pickles Aug 27 '15 at 19:34
  • nevermind, you can just do this, but it won't work, this is why I will never use storyboards, you have zero control over complex views : required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } – Larry Pickles Aug 27 '15 at 19:42
  • Nevermind, it works now, WITH STORYBOARD, here's the solution, use this exactly, one sec – Larry Pickles Aug 27 '15 at 19:52
  • here's the view again: https://gist.github.com/anonymous/0573cbf0db28a6bc03a3 – Larry Pickles Aug 27 '15 at 19:52
  • here's the viewDidLoad https://gist.github.com/anonymous/c753d4e756f2670be74b – Larry Pickles Aug 27 '15 at 19:53
  • Awesome! Works great, can't thank enough for all your help. I also found another possible solution: create another uiview in storyboard (though it could have been done programmatically), set constraints to the container view (the superview of the container view) so that it was 4pts larger than the qr view even though its position wasn't dependent upon it, and set that views background color when I needed it, leaving it translucent when I don't. – Randoms Aug 27 '15 at 23:41
  • Im going to post an answer, woudl you please click if i did in fact answer this right? – Larry Pickles Aug 27 '15 at 23:43
  • Randoms, do you realize how long our conversation is on this one? lol over 100 comments to get this simple thing! I'm so glad you figured this out, I learned a lot myself – Larry Pickles Aug 28 '15 at 00:11
  • I know! It's incredible. I'm so relieved to finally have a solution—two, actually! I know I went from not having a great understanding of constraints, to feeling reasonable comfortable with them. – Randoms Aug 28 '15 at 00:28

1 Answers1

1

Here's the answer:

Do this in your viewdidload:

@IBOutlet weak var asdf = QQCustomUIViewForQRImageView()

override func viewDidLoad() {
super.viewDidLoad()

    var floatiesW = 200 as CGFloat
    var floatiesH = 200 as CGFloat
    asdf!.frame = CGRectMake((self.view.bounds.width/2.0) - (floatiesW/2.0), (self.view.bounds.height/2.0) - (floatiesH/2.0), floatiesW, floatiesH)
    asdf!.qrImageView.image = UIImage(named: "check")
}

Here's the UIView you'll need to use as a subclass, as per our conversation:

import UIKit

class QQCustomUIViewForQRImageView: UIView  {

    var qrImageView = UIImageView()

    override init (frame : CGRect) {
        super.init(frame : frame)
    }

    convenience init () {
        self.init(frame:CGRect.zeroRect)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.backgroundColor = UIColor.redColor()

        qrImageView.setTranslatesAutoresizingMaskIntoConstraints(false)
        qrImageView.backgroundColor = UIColor.yellowColor()
        self.addSubview(qrImageView)

        let views: [NSObject : AnyObject] = [ "qrImageView" : qrImageView]
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-4-[qrImageView]-4-|", options: nil, metrics: nil, views: views))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-4-[qrImageView]-4-|", options: nil, metrics: nil, views: views))
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

that's 4 points on each side, this will resize for you, and the iamgeview is set with an image like I show in the view did load call above

Larry Pickles
  • 4,615
  • 2
  • 19
  • 36