UIViewController completely out of place on the screen - ios

I am putting together a sample iPhone app for practice. It is a drill-down interface with doctors who have patients who have prescriptions and visits using Core Data and a story board.
Everything was going great until I started on the Edit/View doctor screen. Because this screen is triggered by a button in the UITableViewCell, it seemed that there was no way to create a segue on the story board to an edit/view screen because the buttons in the cells are dynamically created.
I decided to make a separate UIViewController custom class and I let Xcode generate the XIB for me to make sure I did not mess up anything. I presented the edit/view view controller by pushing onto the navigation controller. The problem is that the contents of the edit/view screen were nearly completely off the screen. With my first attempt, a navigation bar I put at the top appeared at the very bottom of the screen in the simulator with all of the rest of the text fields and labels not showing at all.
I then created another view controller with xib, specifying iPhone only and simply put a label with "Hello" in the center of the view. When I ran the simulator, "Hello" was centered vertically, but was all the way to the right of the screen with only "He" showing. I tried played with anything I could find to play with, but nothing fixes it. I am not trying to do anything unusual. I want to do everything in the usual accepted way.
Also, it seems like Interface Builder makes the views look like big square iPad screens even when I have specified iPhone. I did check the frame dimensions and it was wxh = 600x600.
Thank you for your help.

It occurred to me to search for 600x600 and it seems that this is the new UIView that Interface Builder provides by default. Because there are many screen sizes, Constraints are apparently mandatory now.
My edit/view form was completely fixed by viewing the XIB and going to:
Editor->Resolve Auto Layout Issues->Add Missing Constraints.

Related

The button size in the simulation is very different from in the storyboard

Like the title says, I cannot get the size of one of my buttons to match that in the storyboard. When I run my program, the button shows up much bigger, taking up a large portion of the screen, which the others don't do. I will include screenshots to show what I mean. I am having trouble finding out how to fix this. I've already tried Clean.
A screenshot of the storyboard, then quiz when it is run
You have to use constraints to make you button or whatever controls looks like what you want on different screens and devices. You should use your storyboard file to put your constraints.
So your button should in the simplest form should be looking in the storyboard file like:
So be sure that all your constraints are blue (blue lines on the screen). They can be orange as well but it is the signal that something can go wrong when you run your project with position or size of your button.

Interface builder position off screen

I have a container view showing a sidebar which is pinned to the main views leading edge. The sidebar is initial visible which is fine for iPads however I would like it to be hide initially for smaller devices. To do that I need to set the side bars trailing edge constraint to be (0 - its own width)
As far as I can see this is not possible in the interface builder. I have tried to do it in the viewDidLoad, checking if the device is an iPhone before doing self.sidebarX.constant = -self.sidebar.frame.width. This fails because viewDidLoad has not set up the views yet so the width is wrong. I also tried to do it in viewDidLayoutSubviews however the user sees the sidebar disappearing which isn't nice. I am sure there must be a common way of dealing with this?
I finally worked it out. viewDidLayoutSubviews was the correct place to be doing this. At first when I tried it, it was showing the sidebar slide away as the view controller loaded. It turns out this is because I was calling my closeSidebar method which animates the side bar moving off screen. Changing this so it just sets the view off screen and adding a check to ensure this only done once on first load (as viewDidLayoutSubviews is called multiple times) does the job of hiding the the sidebar for certain devices without anyone seeing it happen.
You can set this using xcode adaptative layout:
You can set the different position for all different screen types here, changing the constraints, positions, sizes to each different type you need.
You can install the layout of one object in different screen types using the dialog below:
Have a look in this 2 parts tutorial from raywenderlich part 1 part 2

How to display UIView that's outside of screen in main.storyboard?

I'm working on a screen that has a UITableView below the screen(thus invisible) initially, but will pop up when user click on a button. I know it's not supposed to be seen on the screen, but just for the sake of design, is there a way to make it visible when I'm working on main.storyboard?
Please see the picture attached below. It's really hard to work on the UITableView when it's beneath the toolbar. How can I force xcode to display them so it's much easier to work on?
You can't, but you have 3 workarounds:
Move it into view (Xcode will complain that it's not in the right position, but it doesn't matter)
On the bottom left on your storyboard, you can open up the view hierarchy, you can find your element there for creating constraints, linking it with your code and everything else you'd want to do.
Increase the visible size of the viewcontroller on your storyboard, trough selecting your viewcontroller on the storyboard, changing Simulated Size to freeform and increasing the values (will not affect size when running).
The easiest way to deal with this is to:
open storyboard
select the viewcontroller you want to edit
open the Size Inspector
Switch from Fixed to Freeform
Type in a larger height (like 1000)
Simplest thing is to manually do what your button does in the app.
In the view hierarchy on the left, select your table view and move it down to the bottom. This will bring it to the front in interface builder.
When you are done, move it back.
I do this all the time and you get used to doing it. Just watch out for accidentally moving it inside another view when you drag it down or up.

Interface Builder strange interface error

I am having a strange error on my builds. When i design my view in interface builder, everything looks fine. As soon as I run my app on my iPhone, everything messes up. The buttons I use are smushed into the top left corner of the screen, and the font size does not change, even though I have proper settings for all screen sizes. The labels A and B work fine though. This is very puzzling to me. This is not the only view which I am having this error with. Here are pictures to detail my problem further:
This is the view I see in interface builder.
The view I see when I run my app on my phone is quite different.
Why am I seeing the views so differently? Why are the buttons being smushed to the top right of the screen when I run my app?
You probably don't have sufficient constraints on those buttons for the runtime to know how to place them. Usually constraint issues will show up at build/design time with a yellow or red circle in the tree view for the storyboard/xib. Try using "reset to suggested constraints"

iOS xib loaded at app start is not centered vertically

I have an iOS app that loads a xib file for the UI. When I first load the app the interface is about thirty pixels higher than it should be. In the app if I go down one screen and then come back to my main UI it's lined up how it should be.
Right now my view controller is subclassing UIViewController and in the app delegate it loads the nib using initWithNib:
Any ideas what I could do to get the first load to be properly centered on screen?
Within Interface Builder check the actual size of the view in question. I've found when you add and remove simulated elements on a view, such as the status bar, this can cause the height to be reduced but not always increased.
Thanks, that did it. The only thing that you have to watch is if you have the status bar or any other extra bars turned on in Interface Builder, it won't let you change the size of the view.
I tried to vote up your answer, Dave, but I'm too new to.

Resources