iOS layout view to fill device screen - ios

This should be a very basic question, but I have tried a lot of things and it isn't working. :/
I have a single view application which consists of a single, full-screen custom view. I had my view class set on the main view of the view controller, but the status bar was always overlaid on my view.
Apple says to fix this, constrain your view to the top layout guide, but that requires I move my view to be a child of the main view. So I moved it, but now I can't set up constraints to simply make my view fill 100% of vertical and horizontal space.
I have tried doing it in code, using constraints, stacked views, anything I can find, but to no avail.
So, the hopefully simple question is, how can I make a single UIView child of the main view of a view controller size to fill the container and account for the status bar? The UIView is a custom class, so if code is needed I can do that.
I have tried overriding the intrinsic and size that fits methods, but they created terrible results.

You can do this programmatically:
let childView: UIViewCustom = UIViewCustom()
childView.frame = rootView.frame
childView.bounds = rootView.bounds
rootView.addSubview(childView)
Or via IB:
first select your childView, then click on the pin button in the low-right corner of XCode, disable Constrain to margins, then click on each red bar and set to zero the constrain. Finally click on Add 4 Constraints
Hope it helps.

try this:
0.Assume that you have a custom class just for View (not ViewController) named AAA.
in Main.storyboard, drag a "View" from Object Library to your ViewController. Call it ViewB.
2.set the viewB layout and constrains
3.select the viewB and go to its identify inspector(3rd small button on the right panel)
4.in the class section: write it as: AAA
Hope this will help you.

Related

UIViewController subviews added in interface builder not showing up

Ok so basically I just used the interface builder with auto layout to add some subviews and constraints to my UIViewController. My app is supposed to segue to this view when the user wants to take a photo. I laid out a few camera buttons on a black, square UIView that will serve as the camera view once I have programmed it. Since I wanted to make the camera view a square, I also had some extra space where I just made a label that says "Take a photo!" This is how the interface builder for this view controller looks:
And this is how the same view looks when I run the app:
I'm guessing it's something simple that I'm missing that's causing this problem but I will be grateful to anyone who can help me!
When you are using autoLayout you should set all needed constraint to the view. Otherwise it is an ambiguous layout problem, and the system is not knowing where to set your view
If you want the height to be the same like the width set the aspect ratio constraint to your view
And be sure that the multiplier of constraint is 1
Bring that subview to front, putting all other views to its back
try it will work
parentView.bringSubview(toFront: childView)
This method can also be used to bring any subview to the front, even if you're not sure where it is:
childView.superview.bringSubview(toFront: childView)

AutoLayout Not Working For 5.5" iPhone

In my app, I have a screen that needs privacy, so when you first open it, it will check if you have logged in with the password. If not, it adds another view on top of the SuperView until that password is entered. I use XIB file for this. The main View of the screen is a tableview, and I drug on another UIView to use as the Password screen. However, despite AutoLayout, the Password view is only taking up the dimensions of 4.7" phone, meaning you can see behind it on the edges. I made sure AutoLayout is enabled, and on the background image, which is the gradient, I added 4 constraints, one for each side, set to 0, so that it would always cover the view behind it, and then set the UILabel to be aligned horizontally in container. What am I doing wrong? This works PERFECTLY in a different view and XIB within the same app, and I even copied the view straight over, but it is still messing up on this one class.
Code to load this is (password is the UIView declared in the header as IBOutlet UIView *password:
[self.navigationController.view addSubview:password];
The correct solution is to use a view controller to manage the curtain view, and either push that view controller onto the navigation controller's stack, or present it from the navigation controller or the tab bar controller.
There may be 2 possible reason
you may not applied constraint on your xib.
you have applied constraint on it but when you are initialising your xib you are providing fix frame.
it would better if your share your code how you are doing it ?

UITabBarController: Tab bar covers view

I am fairly new to UI elements in iOS (all of my apps were Gl games) but I'm trying it out and I found myself in a bit of a pickle...
The view controller for the bulk of my app is a subclass of UITabBarController and I have a few tabs with (currently empty) child view controllers. For the most part I have these working fine, but my problem is with correctly sizing and placing items inside of a tabbed view controller. If, for instance, I try to place something just above the tab bar, then I can't just set the Y position to the height of the the child view controller's view minus the height of the object I'm placing. I also need to account for the tab bar itself.
I can see that part of my problem is that I'm simply creating the view controller and not telling it what size it's view needs to be. Is there a function provided to me to calculate the height the view needs to be? I could just figure out the height of the bar and subtract that from the overall height, but that just feels to flimsy to me.
Any ideas?
EDIT:
I am doing it all programmatically and I don't intend to use Interface Builder. My main concern with the view size is making sure that things like table views fit snugly.
If you are using Interface Builder and auto layout, you can place UIView items on your UIViewController and if you set a size constraint on the view you're placing you can also set position constraints that are relative to screen edge or adjacent views, in your case UITabBar.

Placing a control over a mapview iOS 7 using storyboards

I'm trying to place a button over a mapview. In storyboard I have it as the last control in the view hierarchy, the map view still covers it up. I've tried adding this line in my viewDidLoad method to bring my button view to the front but it doesnt seem to work either
[self.view bringSubviewToFront:self.flagButton];
anyone have any idea why this wouldnt be working? the map view covers most of the screen, and I want to place a few buttons on top of the map, but so far can't get them to show up, they are under it every time.
I was able to do this by making my view as subview of map view.
first,
self.view = self.gmsMapView;
then,
[self.gmsMapView addSubview:_myBtnView];
I know this is an old post, but I'm answering for the sake of someone facing the same problem.
I was having the same issue, then I found a solution, and is as follows:
First, open your project and go to your StoryBoard then click on "Show Document Outline" switch that is on bottom left very small switch. After that select your "Scene" that you are having problems with then click "View". Find your MKMapView object and drag it up until you reach the first row. This will make your MKMapView object (or any other) "the first in a layer" so to speak.
I hope someone will find this helpful.
E
I was using storyboards and was not seeing the button. I had to set the MKMapView to hidden until the button was displayed and in position. Then I set the MKMapView to visible.
Here are the steps I had to take in the interface builder to get my button to show
Create a UIViewController which contains a view
Add an MKMapView and set constraints
Add UIButton in the same view as the MKMapView, set the image and text, whatever is applicable. If MKMapView disappear when you try to resize or move the button, the map view has just set its width and height to zero. Enter new values in the Size Inspector for the map view and it will resize back. Or you can move the button elsewhere off the MKMapView and then move the button back to where you want it.
The important part is to set constraints for the UIButton. I also had to set the MKMapView hidden until I got the size and placement right and then set it back to visible.
Here's how it looks in the simulator
You can't add a subview to an MKMapView via the storyboard. Add it as a sibling view instead.
Edit: Your map view should also be a subview of the root view (i.e. self.view) for this to work.
I was having similar issues and I did the following:
Made sure both the map view and the button are at the same level in the view hierarchy, I accomplished this by creating a View that both the map view and button were children of.
Made sure the button is dragged below the map view in the left gutter of the storyboard (so it is actually rendered on top).
Made sure there were valid constraints for the button.
The only thing new that hasn't already been mentioned was double-checking the constraints, and that appeared to resolve it for me. Hope that helps.

Add a ScrollView Manually or Programmatically

I have an existing view with several elements, I was thinking of how to include a scrollview to contain all the existing elements without having to go through redesigning one by one the elements,
My objective is, that when my orientation changes to landscape, the elements behind can be scrollable specially when a keyboard is currently displayed.
Can this be done manually or programmatically, and how? Or is there any best approach for this type of scenario's?
*Edit (for clarity)
My apologies for the lack of clarity with the initial question;
I have a view with 3 Textfield, A button and 2 switch, Initially my app only supported the "Portrait" orientation, however, recently I have decided to add "Landscape" orientation support, I already did this. Now my problem is when I'm on landscape the view elements gets covered by the keyboard (specifically). So I was thinking if I could include a scrollview and put the views existing elements(objects) without having to re-design the nib, what I have done already;
I drag a scrollview into the view then making all the existing elements as children of the scrollview so the hierarchy looks like;
--view
--scrollview
--textfield
--button
The end product I get with this approach is that all the elements stacks on top of the other, so I've lost my original design.
So this is what Im asking, is there anyway, to add a scrollview without having to go through the design phase again.
TIA
If your view is designed in a nib, from IB you can select all the elements of the view and then go to Editor->Embed in->Scroll View from the menu.
Following are the steps-
1.Add the scrollview to the main view as subview.
2.create a new view which contains all your elements for the scrollview.
3.add the new view to the scrollview as subview.
4.set the content size of the scrollview as the size of the new view.
Change the frame of the newView accordingly on orientation and repeat step 4.
Thats all you need to do..

Resources