Extra space on top and bottom of Init View Controller - ios

I have a swift app I have been working on for some time now. I noticed that my init view controller has extra space at the top and the bottom of the view, so much that it allows me to pull up and down on the first view and let go and have it spring back into where it was. I removed all my constraints thinking I had an issue with them, but it's still done the same thing. The size class is set to any any, and all my graphic fit inside the view, yet I still an able to pull up and down.
I also made sure my view, wasn't a scroll view, it's not.
I looked around for similar issues, but couldn't find anything like this.
Thanks for the help!
Here is what my view looks like in IB
When I run the app.
Showing the amount I am able to pull it down from the top.

If you remove all of your constraints, and the storyboard is set to use auto layout, it usually won't show up correctly, as you have seen. You will have to add the constraints to each element in your view controller. To do that, you can control click on each and drag it where you want to connect a constraint. Or, use the pin and align buttons in the bottom right corner of the storyboard view.
There is a good tutorial from apple that I suggest you read. It will help you with the basics of using auto layout. Another option would be to not use auto layout. To turn it off in the Storyboard, go to the file inspector and uncheck the box "Use Auto Layout".

Related

Placing views and elements within Scroll View

So I'm trying to get a layout to work using Scroll View.
Correct me if I'm wrong but from what I've been reading around the internet, it looks like to properly use Scroll Views and make it work with AutoLayout you need to have your root view, then put the scroll view inside it with constraints binding it to take the whole size of the root view (left/right/top/bottom constraints to 0) and then adding a view inside the scroll view and once again binding it to take up the whole space.
Afterwards, any ui elements or subviews would go in the innermost view.
My problem is that often I have experienced issues while settings my constraints within the scroll view where for example setting a trailing constraint to 0 wouldn't actually set to the end and so on, the numbers just wouldn't add up. In this case when I'm trying to make the innermost view take the whole space,it ends up messing it up more than anything; see image below.
Any help would be appreciated.
I just created this layout using the follow:
To test it, I added an image view to the content view and put a humongous image in it. It scrolls around quite nicely including bounce etc.
Update: You need to select your ViewController and uptick "Adjust Scroll View Insets"
Hope this helps.

What are the best practices in building user interface with Xcode?

I am designing a simple user interface for an app using Xcode and Auto Layout. I have a view controller with three basic outlets: a static image view, a label and a button. You can see it in the picture shown below.
As you can see, I'd like to have the image view horizontally and vertically centered in the view controller, the label just below it and the button somewhere in between the label and the Bottom Layout Guide.
The problem is that I am experiencing some appearance problem with the iPhone 6 I am using for testing. The interface items are not nicely centered as they are supposed to be, as you can see from this screenshot.
I am not asking for the solution to this problem, though. I would rather know whether there are best practices to design a good user interface. For example, is there some way to temporarily hide the label and the button from the view controller to focus only on the debugging of the image view without having to remove and redesign them again?
Select the label you want to hide,then uncheck the checkbox installed
You can simply set its alpha to zero .

ViewController gets misplaced, when a view size is adjusted

I'm quite new to the Xcode programming. I've added some view controller, labels, buttons and image view to my program, but, when I try to adjust, for example, the size of a label, the view controller that that label is in, gets displaced. I mean, when I try to drag the side of the label to change its size, the view controller moves down, making my label get displaced in the view controller. It is a very weird situation, here you have the screenshot of my project, the view controller misplaced is the one that says "Perfil" on the navigation bar. Every time I try to change one of those light blue views size, the entire view controller changes place. The more I try to change the side the more it will go down.
Sorry for my English, not my native language.
I think the bigger issue is that you are using Table View Cells that are not part of a table view.
Table ViewCells are supposed to be a part of a UITableView and not just stuck in a view.
Using UITableViewCells in this way is what is causing the strange behavior.
If you are trying to make a table, put a UITableView in your view, and then you can customize the table view cells in there.
It you aren't trying to make a table, just use UIView's instead of those UITableViewCells.
I have two ideas that may fix the problem:
First, if you're trying to drag and drop the label but the view controller moves instead, then you might just be grabbing the view controller's background by mistake. When this happened to me, it was because the order of my components in the xib file was wrong. Things are ordered from back to front, and when you click and drag something, you'll just be clicking and dragging whatever's on top. You can try and get past this by clicking the UI element on the left to be sure you select it, then dragging the handles.
It looks like you have things in a reasonable order, so that may not be the culprit at all.
Second, if that doesn't work, select the element you want to edit on the left, then open up the utilities pane on the right. Click the Attributes icon (should be fourth from the left) and look towards the bottom. You should see something like this:
Just manually edit the position and size of your labels there. You should see the change immediately reflected in the display.
Hope this helps!
Setting no autoresize behavior in Interface Builder solves the issue.

iOS: How can I prevent a resized view from resetting itself?

I'm using ECSlidingViewController. When the app starts, it opens a scroll view which is resized dynamically, since it contains both labels and a table view. Both the table and the scroll view containing it are resized.
Everything works as intended when the view is first loaded. However, if I open and close the menu view, or if I use it to go to any view which resizes itself, the resizing does not work. Instead, it displays the scroll view as if it has the height assigned to it in the storyboard.
I'm completely lost as to why this happens, and would greatly appreciate help. I can post code as well, of course, but I don't know what code might help, since I guess that the error is on ECSlidingViewController's part.
Are you using auto layout? If so, whenever something else happens that triggers the application of the constraints, all the frames will be reset to the values dictated by those constraints. Try turning off auto layout and see if that fixes it. Refer to your auto layout settings.
Alternatively, if you want to keep auto layout, I'd generally suggest changing the frames by programmatically changing the constant values of the constraints. But, I'm not familiar with ECSlidingViewController, so I don't know if that's a reasonable option in this particular case.

Designing inside a scrollview in xcode 4.2 with storyboards

I have a vertically scrolling uiscrollview - imagine an 'about this app' page of a tab bar app which goes on a bit and requires a scrollview. It only contains a few images, a video and some text (only the video has been coded in - the rest have been placed in the GUI). In storyboard (Interface Builder?) Xcode 4.2, everything is set up as it should be and works fine, but the view is only as large as what you see on the screen, is it not possible to manually arrange in storyboard the items that are initially offscreen - that you need to scroll up to? The only way I've found so far is to design them on the visible view then navigate them down with the arrow keys..
In the storyboard select the viewController, then in Attributes inspector change 'size' to 'freeform'. Then change the 'height' of the view/scroll view to as big as you need. The default settings of struts and springs should take care of resizing the view back correctly when the app is run, but you should double check.
I feel your pain. The only way I found is to manually pan the scroll view in the size inspector to reveal the portion of the view that you wish to visually edit.
Use a UIView to contain elements so they are positioned relatively to this view. Add the view as a subview to the scrollview at 0,0.
pan: use the Y coordinate say to -200, then edit the contents.
to place more contents in the hidden part, pan again to reveal new real-estate
when finished, restore the values of the ScrollView's height and X,Y position.
Make sure the scroll view frame rectangle is smaller than the contained view.
New: 3/26/2013
I stumbled upon what I think is even simpler way of dealing with UIScrollView directly in storyboard.
No code needed, just storyboard settings. This maybe new in iOS6.1 / Xcode 4.6
No need to disable constraints (i.e. uncheck "Use autolayout" in File Inspector for storyboard file)
No need to add UIScrollView* scrollView; in .h
No need to add self.scrollView.contentSize = ... in overrides of viewWillAppear or viewDidLoad
Here is what I did (important parts highlighted with **): (see code)
Create a new project with storyboard enabled
Drop in a UIScrollView, set class in identity inspector for view controller
In attributes Inspector, change Size under simulated metrics to Freeform**
Select scroll View; In attributes inspector, turn on "scroll enabled" and "background" to "White" (you'll figure out why - if you don't)
Under Size Inspector (with scroll view selected) change the height to 900 for example**
Add buttons, one on top and one at the bottom
Add a default handler for buttonTouchUpInside for both buttons and simply Log sender.
See Code Select the View Controller and scroll view and check inspectors.
Just change the 'Simulated Size' of the view controller to freeform and set a height that is larger than the usual size, you will be able to see all the outlets you need to edit.
On iOS 6.0 you can drag a Container View inside your Scroll View. This will automatically create a new View for your content, outside of the current scene. You can then resize this view as big as needed to fit your content.
I believe you would still have to set the ScrollView content height at runtime, but at least you can design you content view at once without having to scroll up and down on IB.
Just uncheck the "Autoresize subviews" from any view that you're trying to resize and it should keep all your objects from resizing with it.
I've been struggling with this for a while now, and every single thing I've tried has failed.
Specifically, What I am trying to achieve is a freeform sized modal dialog with a scrollable view containing a container for another view. I have had a lot of varied results, including occasionally having it working correctly. Most often I get it looking exactly correct, but with no scrolling.
In finally downloaded Dickey Singh's code, which worked perfectly but had nothing special. (Excellent clean solution BTW). So, I added a container view to it, exactly as I had in my code, and it broke!
After some experimenting, I worked out what is going on. Just bear with me.
1) Using Auto Layout, the size of the scroll view seems to dictate what the scrolling bounds will be. Setting "contentSize" in "USer Defined Runtime Attributes" seems to have no effect on this, and neither does setting "contentSize" or "bounds" in "viewWillDisplay" or "viewDidLoad". Thus if the initial size of the scroll view is 800x800, that will be all the space that can be displayed. For this reason, when I want a scrollable region, I create a container view and then put the scrolling view inside the content.
2) Without Auto Layout, setting "contentSize" in "User Defined Runtime Attributes" works, as does by setting it programmatically in "viewDidLoad". I prefer to use "User Defined Runtime Attributes" because it keeps the size with the layout. This solution allows you to use scrolling view with more flexibility, since it can be any size at design time.
3) Regardless of Auto Layout, if any view within the scrolling region exactly matches EITHER the horizontal or vertical frame bounds, then the scroll view ceases to function as a scroll view. This applies to my own code and to Dickey Singh's code in every possible configuration that I have tried.
I have no idea what is causing (3), but it is clearly a bug.
I hope this helps everybody out there who is struggling to use scroll view. I imagine that some people are using them without any problem, and some (like me) have had noting but problems with them.
Here's my solution to design a ScrollView with a content larger than the screen entirely in Storyboard (well, except for 1 single line of code :-) :
https://stackoverflow.com/a/19476991/1869369
I'm currently developing an app for iOS 7, and I did exactly as #Dickey Singh's answer, but it doesn't work in the beginning.
After checking the storyboard, I found that we also need to add Auto Layout Constraints for the view controller who holds the scrollView.
It seems that such auto layout constraints would be added automatically before Xcode 5, but now we need to do it ourselves.
The way to add constraints: First select the view controller in the storyboard; Enter 'Editor' in the top menu; Select the 'Resolve Auto Layout Issues'; Select the 'Add Missing Constraints In Container'. Done :-)

Resources