I've been developing apps in Xamarin Studio for ios devices (iPad, iPhone, iPod) and I have a problem regarding child UIViews width. I want a child UIView to have the same width as its parent, but I am unable to figure out how to do it. I am using Auto Layout on my screens setting the necessary constraints, but this only keeps my widgets on screen organized (it doesn't auto resize anything for different screen sizes on iPhone). Can someone help me achieve this? I need UIView same width as its parent.
This is how the screen looks on iPhone 6 plus
This is how the screen looks on iPhone 6
To be Honest, I normally do constraints in Xcode as it is easier. Right click on your nib and select Open With > Xcode Interface Builder
Then once your nib is open ctrl click and drag from your top container view to the superview and select Equal Widths then do the same for Leading space to container, Top space to container, for height ctrl click drag on to itself(top container view) and set the height.
If you're having difficulties it might be best to upload the nib to github so I can have a look.
Related
This is a picture of a UIView in another UIView with View 2 nested in View 1. Note that I cannot change the size of view 1.
This is what happens when view 2 is dragged outside of view 1; it forcibly turns into an iPhone XR, again, with the width and height properties grayed out.
I would like for these views to just be normal UIViews that have the ability to have their width and height changed. Any ideas as to why this might be happening? Am I missing a setting or property that's causing this to happen?
You have the View as: option set to iPhone XR. To change that, click on View as... at the bottom and change it to a different device (preferrably to one without a notch, such as the iPhone 8).
Here's how to do that (click on the images to enlarge them):
To change the size of a top-level UIView, follow this guide.
I'm trying to recreate the iOS stock calculator app using UIStackView in iOS 9.2 with Xcode 7.2.
I succesfully created the portrait version, now I was working with Size Classes to implement the landscape one.
The approach I used to create the layout it was to have:
a UILabel for the display, which has leading,trailing and top constraints from the superview
a UIStackview as container just below, which has leading, trailing and bottom constraints from the superview and the top from the UILabel. This Stackview stacks its components vertically with "distribution":"fill equally" and "alignment":"fill"
inside this UIStackview, one UIStackview for each row of buttons. These stackviews stack their components horizontally with "distribution":"fill equally", "alignment":"fill" and "spacing":"1" (to recreate 1pt black border)
As you can see from the screenshot here (Storyboard Screenshot), the result I got on the storyboard it's perfect: all seem to working fine and Xcode don't give any error neither warning!
However, when I compile, the result on the simulator (Simulator and Debug screenshot) it's completely different.
The buttons, in particular the ones present only in landscape, seem to go outside the stackview for some reason (see the screenshot above for the View Debug).
Anyone have experienced this problem? It's really really weird because I think the logic behind the layout it's ok.
Thanks
I am a little bit confused by Xcode 6 Storyboard.
The auto layout seems to be a very powerful tool to build UI for different devices. However, no matter what kinds of layout I chose (compact or any height/width) or even uncheck the "Use auto layout" and "Use size classes", what I mapped in the storyboard view always looks different when I am testing (using an iPhone 6).
For example if I left a UIButton at the center of the storyboard, it will be a little right down away from center when appears on my iPhone 6.
Could anyone please give me some help on this. Maybe just for iPhone6 instead of different devices. Thank you very much.
If you simply want to center an UI element check both Horizontal Center in Container and Vertical Center in Container in the storyboard constraints alignment popover.
The reason, you are having this issue is the mis-matched stimulator size. For example, the apple standard for iPhone 6 is a frame of 400 x 600 pt, meaning 2:3. So,
First, turn on the auto Layout and size classes. Then change "wAny hAny" to "compact hAny".
If, you change the Stimulated Size now, you will se the width as 400 and height as 600.
After this you need to add the UIButton at the centre and even without adding any constrain, you will see your button to be centered in the frame.
Using Xcode 6, I am building an iOS app that targets iOS8.
I'm working on a LaunchScreen.xib file, created from the menu: File > New > File > iOS - User Interface - Launch Screen. "Use Auto Layout" is enabled.
Inside the XIB's main UIView, I've placed a UIImageView. I would like to add width and height constraints, so that the UIImageView has the same size as the container. However, Xcode doesn't let me do that: I can only work with spacing and alignment.
Here's what I see:
What am I doing wrong? Why can't I see a menu like the following?
I know I could reach the same result by setting leading space, trailing space, top space, and bottom space to container to 0, but I would also like to understand what's happening here.
EDIT:
Xcode 7+ defaults to a Storyboard file for the Launch Screen.
As explained below, unlike XIBs, Storyboard files allow you to set width and height constraints to the root UIView.
I haven't been able to find an official explanation as to why XIBs behave like that, when they have a UIView at the root.
What looks weird to me is that what we are provided with as a default (LaunchScreen.xib) isn't set up to behave exactly like the Storyboards we've got used to.
The first solution I opted for was using a Storyboard to draw the Launch Screen, instead of a XIB:
Delete LaunchScreen.xib
Create a LaunchScreen.storyboard (Menu: File > New > File > iOS - User Interface - Storyboard)
Go to your project properties, select navigate to your target's general properties and select LaunchScreen.storyboard in the "Launch Screen File" combo box
Open LaunchScreen.storyboard, add a View Controller, and make sure to tick the "Is Initial View Controller Box"
You can now work on the View Controller's View as your Launch Screen, and you'll get the width and height constraints I was initially looking for
An alternative solution is removing the default UIView in LaunchScreen.xib and replacing it with a View Controller.
Just like in the previous case, if you do so, you can work on the View Controller's View, and you'll get the usual constraint menu.
That happens for every .xib, you just can't make a view of equal width than the main view. On the other hand it is possible to do it when you are in a story board.
Finally as you said you can accomplish the desired layout by setting the leading, trailing top and bottom distances.
Will your app run on a single device? If not how do you know the size of the container? It will vary between an iPhone 5, iPhone 6, iPod or iPad. The best way is still to specify leading space, trailing space, top space and bottom space to 0. You're sure it will work on any device.
Anyway, I tried to reproduce the problem, but I am able to specify the width and height of the image view when going in the Pin menu whether in Editor/Pin or by pressing the button at the bottom of the editor. I can specify any width/height with no problem.
Hope that helps!
A very simple solution is to add a clear UIView and pin it to the container, then you can access all the constraints by associating them with the clear "root" UIView you have added.
I build a view that has contents which exceed the height of the iphone 6 screen vertically.
To test that out, I've set a specific height for my image (600pts) to guarantee that the whole content wouldn't fit on the screen of an iphone 6 and that you should swipe down to see the rest (the label "BYE").
However, when I run the simulator, I cannot swipe down to see the rest of the screen.
I've used Autolayout constraint and set a margin top/bottom between each components and the superview (I don't have any autolayout constraints issues/warning).
Here's the screenshot of the storyboard of my UIViewController:
Here's when I run the simulator, I cannot see the Label "BYE"(which is normal) but I cannot swipe down to see it.
I am using Xcode 6.1 and Swift.
Any suggestion and explanations?
Thanks a lot!
I have redone your screen and here are the steps. The reason for your issue is not setting proper constraints in autolayout. First you need to choose AnyWidth AnyHeight in Xcode which is in bottom of the storyboard.
Here is the screenshots of View in StoryBoard and its constraints
Constraints
Final Result in Simulator
Setting the constraint properly will work for you. Let me know if you have any issues in doing so. Sorry for the image i used its low quality one and its just for test.
EDIT
In Storyboard if you see the bottom of the screen you will see below image. In center of that image there is wAnyhAny.
you need add constraint to thoes components!
Otherwise, some components will be placed out of the screen!
You can learn how to use Autolayout from here
![enter image description here][1]At right bottom of storyboard in xcode you will see triangel Resolve autolayout isuue select in Reset to suggested constraint.