How to display label on top of UIImage? - ios

I have been trying to add a label on top of an image but the label is under the image and I've tried turning AutoLayout off, adding constraints and moving tried moving the label on top of and on bottom of the UIImageView in the View controller scene but its still under neither. Anybody know a quick fix?

Your view hierarchy looks OK (i.e. the label is further down in the view hierarchy than the image view, meaning that it will appear "over" the image view). If you're not seeing the label, it either doesn't have text in it or the constraints are not defined properly.
I'd suggest running the app through Xcode and then clicking on the "view debugger":
Then you can examine where precisely the label is (confirming that it is above the image view) and what its frame is (i.e. perhaps your other constraints are not defined properly) and what text it contains (perhaps it's in the right place but just doesn't have text to show).
You can either navigate through the view hierarchy in the left panel or rotate and expand the wireframe rendition in the main panel.

I guess that is due to your arrangement of elements and constraints in IB. Try removing all the constraints and see if you have the label on top of the image. If so - add constraints gradually and observe not to launch your label off the screen.

Related

Stack view issue in iOS

I have two labels and one image view (the "sound" image) in a stack view. Actually, the top label and image view are in a horizontal stack view which is inside another vertical stack view.
I want the "test" label to be as close to the image view as possible, however, as you can see in the image the "test" label stretches out as much as it can so that the "test" + image view width matches the width of the label below. How can I fix this?
I know I could use some fake transparent view in the top horizontal stack view to push the image view as close as possible to the test label but I'd rather avid hacks.
That should be pretty easy.
Interface builder:
Result:
Settings:
Give your test UILabel a height if you want to make it good, or equal to the button or imageView, but as I can see in your screenshot, you already did what specifically what you wanted to do, except the settings of your vertical (main) UIStackView.
Play with your stackView settings. In my vertical (main) stackView, my Alignment is set to Leading. Setting it to the default Fill will achieve the unwanted result like in your screenshot. Distribution is up to you.
I hope this helps! :)

Too much text pushes my buttons below the screen

So I tried embedding my label (that has all the text) and buttons in a scrollView to perhaps be able to scroll down to them, but when I do that, everything just disappears.
my storyboard
my preview
Just added some pictures as requested (thanks).
As for adding the scrollView (which may not be the optimal solution), I just selected all 4 buttons and the label, then selected "embed in scrollView". This led to a completely blank screen in preview.
It sounds like you need to fix the bottom constraints of your scrollview, so that the scrollview's frame does not extend past the bottom of the screen. You can then set constraints on the scrollView's content size to allow the user to scroll.
From the storyboard image you posted, it looks like there isn't a bottom constraint. Select the scrollview in the storyboard, click the pin icon, and put in a number in for the bottom constraint (e.g. 0).

Xcode 6: How to make an image view fill the screen on all devices? Auto layout not working?

So I have an image view and a button. I need the image view to be the correct size (not look squished or stretched) on all devices and more importantly I need it to be the size of the screen. I need the button to stay in about the same place on all devices.
Back with Xcode 5 I remember auto layout fixing this problem but now that does not work. I looked at https://www.youtube.com/watch?v=G53PuA_TlXk and tried control-dragging to the view controller and centering the view both horizontally and vertically, but I can't get it to scale to the size of the screen. The video doesn't address this.
Is there a specific option I need to choose when control-dragging to keep the image view scaled? What should I do?
You need to do 2 things:
Set up your constraints properly
Choose the type of scaling you want.
Setting Up Your Constraints:
Drag out an Image View and place it roughly in the center of your view.
Set your size class to wAny hAny.
Click on the Pin icon |-[]-| at the bottom of the screen.
Turn on all four orange I-beams. Set the four constants to zero.
Uncheck the Constrain to margins checkbox.
Click Add 4 constraints
Choosing Your Scaling:
Click on the Image View you added to your View Controller.
In the Attributes Inspector on the right, choose your image.
Set the Mode of the View to Aspect Fill (fill entire screen cropping top/bottom or left/right as necessary) or Aspect Fit (letter box image so that entire image is shown uncropped).
You can view the constraints that have been created for you. In the document outline, you should see 4 constraints under your image view:
Click on a constraint and then view them in the Attributes Inspector on the right. Here are the 4 that I created:
This is what worked for me:
View Mode:
Aspect Fill
As constraints for the image:
Align Center X to: Superview
Equal width to: Superview
The 'Equal width' property is the one that scale the image to the size of the screen.
If you want to have the button in a specific position, set constraints from it to the view, the main ones you want to consider are a top constraint and any left or right constraints. So you are telling the view where you want to position the button in relation to its sides.
Hope this helps.
Not sure I understand "exactly" what you're after but if you want to drag an image view into the controller, and that image view to automatically fill the whole view controller background: just drag the image view component into the view controller window.
Thing is: if you see a blue bounds line around the view controller, before you drag, the image view won't fill the view controller.
You need to click anywhere on the background in the view controller pane, so the blue bounds line disappears, only then, will the image view you're dragging in, fill the whole controller background automatically.
If you find, using previews, your pics are getting squished or stretched, then, just go to the "Resolve Auto Layout Issues" menu, and click "Add missing constraints" - then all should resize to look ok on all screens.
In my constraints, I needed to uncheck "relative to margin".

Implicit Constraint iPhone Portrait Screen Height

I tried to create a custom freefrom view with interface builder.
I have a UIView which is containing a UIImageView and UILabel.
I set the constraints to adjusting the Label according to the Image etc and a margin to the superview.
Now I want the superview to exactly fit the subviews. But when I press Editor -> Size To Fit Content, the superview is not resized completely and the constrains are in conflict. What there appears is a implicit constraint ("iPhone Portrait Screen Height"), which can not be altered/deleted. See Screenshot below:
While not ideal, depending on how your XIB is structured and how you are referencing the view, you could create a "scratch pad" view and place your custom view inside of that just to see how everything will layout. That is if you're setting the view to an outlet defined in the XIB's File Owner, you can just connect it to the view you've created inside the "scratch pad".
For example, I created a container view with a square and a switch inside. The top example complains of conflicting constraints and doesn't layout correctly, while the bottom example shows the same thing centered in a throw-away view. Again, not ideal, but it's working for me.
Edit: I just realized that some size class constraints may be lost once the "scratch pad" view is discarded so that is another wrinkle to consider. Still looking into this.
Just to make it simple forget the label for the time being. You have already added the width and height constraint of the image view, now instead of using sizetofit content, try adding leading, trailing, top and bottom constraints between the image view and it's parent view. With that your parent view should fit the image view.
The same technique (constraints to parent view) can be used to get it work with the image view and label together.
I noticed that when I physically dragged the superview's width and height to satisfy the constraints, the red constraint errors went away along with the "iPhone Portrait Screen Width" constraint. In other words, my constraints dictated that my subview be bottom aligned with its super view. If I dragged the superview's bottom edge up to align flush with the subview, the errors went away. Still seems buggy and unintuitive.

How to use ScrollView and ContainerView with Autolayout checked

I have trouble implementing scrollview with auto layout:/
Here is simple "Hello World" project with that problem: http://www.sendspace.com/file/cg96by
But the problem… I need to create scrollview, but also I need to use auto layout. So I created pure single view application, added (like I saw in some tutorials) Scrollview (the same size as main view), added constraints (all zeros) and then add Container View (the same size as scroll view), add constraints and.. It's didn't work:/ It doesn't scroll. Here is the screen of that situation:
Now, when I delete two constraints named: Center X and Y Alignment (marked on the screen), it works perfectly, but Xcode5 doesn't quite like it :/ and shows some errors like on the next screen below.
Why does he want width and heigh 0 ?!
Actually ContainerView doesn't matter. I've also tried with image view. When you select "Add missing constraints" it also add Center X and Y Alignment. And scroll doesn't work. When you delete them, scroll works but you have this warring:
Ok, I find manually adding the autolayout constraints to be quite confusing, using the "Add New Constraints" button at the bottom really comes in handy, try this, after you get each view where you want it use that way of adding layout constraints for each view (ie scrollView, containerView, UIImageView) I did this, and heres the example, select each constraint as I have so that the top, left, bottom, and right edges are set, this should do exactly what you want.
You will probably want to erase all your current constraints and then implement them this way.
heres the layout like you want http://cl.ly/image/463k2043401L
and heres the example adding the constraints: http://cl.ly/image/472l2V0l3N1L
To keep it dynamic so that you can rotate it, you will want to do the above for all view BUT the container view... then If you control drag from container to scrollview with no constraints, you can add all of them that it suggests except for the height and width at the bottom, that satisfies any missing constraints and should do the trick, It will center the container inside of the scrollview and keep it pinned to the top, bottom, left and right, and it should dynamically change with your scrollview content size.
http://cl.ly/image/1o3k1e452W0g
Sorry for the confusion, hope this helps!
I had a similar problem and i found relative simple solution from within Interface Builder using pure Autolayout without any code.
For proof-of-concept at first remove any constraint in View Controller to if see this works.
This is sample layout:
View (main view of my UIViewController)
Scroll View (UIScrollView)
Container View (UIView)
Content View (e.g. UIImageView)
A. Scroll View width/height should be smaller that Container View width/height
B. Container View should have some determinated width/height (may be explicit width/height )
C. Do Control-drag Container View to Scroll View and add only:
Leading Space to Container
Trailing Space to Container
D. Check out those two constraints and set "constant" value for both to 0
E. Run app and

Resources