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.
Related
I am building an iOS app with multiple screens but I want avoid duplication of code as well as one large storyboard. Ideally I would like to load the various View Controllers based on a selected storyboard in my content view when needed.
The template of the screen (Master/Root View) shall be composed by 3 Views, namely:
View A that acts as a Navigation View,
View B where the various View Controllers should be loaded based on user actions and,
View C, much like a Tab bar or a 3rd View where I can display some
circumstantial information.
Now, of course I could go with a traditional UINavigationController and UITabBar but I need greater control over those views, notably in terms of size (they have minimum heights that are larger than the ones for NavBar and TabBar).
I also need those Views to resize based on the Traits and Class Size to keep filling the entire screen.
I am trying to use container views to doing so, but I can't get them to resize automatically despite having tried a different set of constraints in auto-layout. Putting the 3 views in a stackview does not do the trick either.
Ultimately I would like to be able to work separately on my various Controller Views that will be loaded in View B, using their own storyboards, calling them programmatically in B and having everything automatically resized.
Thank you all for your kind and valuable input!
Chris
When using addChildViewController(childVC) / addChild(childVC) (Swift 4.1 / 4.2), followed by myContainerView.addSubview(childVC.view) you need to either
add constraints for the added subview
or
set the frame of the subview and .autoResizingMask
After that, your newly added subview will layout and resize correctly.
If a Table View Controller is created using Interface Builder, on iPhone X the lowest visible cell will be obscured by the home screen indicator, and the cell's corners clipped by the curved screen, by default - see screenshot below.
If I use a View Controller and insert a Table View, then set it up by hand, I can use the bottom layout guide with the table view, to ensure this obscuring and clipping behavior doesn't happen, i.e., by not allowing the table view to extend into the curved part of the screen.
I'm upgrading a few legacy apps, and I'd prefer not to have to convert the existing Table View Controllers into View Controllers if possible. How can I make Table View Controllers created with Interface Builder behave?
According to the Apple docs, by using the standard interface elements this shouldn't be a problem.
Inset essential content to prevent clipping. In general, content
should be centered and symmetrically inset so it looks great in any
orientation and isn't clipped by corners or the device's sensor
housing, or obscured by the indicator for accessing the Home screen.
For best results, use standard, system-provided interface elements and
Auto Layout to construct your interface. All apps should adhere to the
safe area and layout margins defined by UIKit, which ensure
appropriate insetting based on the device and context.
If you enabled automaticallyAdjustsScrollViewInsets, table view will be upon home indicator when you scroll to bottom. I think this is what Apple wants, take advantage of the full screen at the same time every elements can be interacted.
If you want cells never obscured or clipped in UITableViewController. I suggest you use plain style table view, set contentInset to make section footer hovered under home indicator. I tried this, It worked but looked ugly. Like this:
In the end, I converted the Table View Controllers into View Controllers and added a bottom alignment constraint to the Table View
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.
I designed about 40 view controllers using a 5.5 inch storyboard layout. After all of that I tested it on the iPhone 4S...big mistake. everything is jumbled together being for a larger screen size. I was able to fix one view controller up using Size Classes. I am wondering if there is any way I can adjust all 40 at the same time, or at least avoid doing this for every single one. It is really frustrating finding this out now. Thanks!
This is a relatively complicated issue you are attempting to solve, but I have two potential solutions. Both suggestions are based on moving your current interface into containing UIScrollView instances
If you are using storyboards, then for each of your view controller scenes, put a UIScrollView as a descendent of the view controller's view. From there, provided your subviews are contained within other views (like a container view for a set of buttons), you can move those into your scroll view. You will have to setup constraints to define the size of the scroll view's content, but this will allow the size of the device to have a smaller impact on the interface as you will get scrolling as needed.
If you are using nib files (.xib) then it is essentially the same thing, but easier. In this case, move a UIScrollView onto the canvas, but not as a subview of the default view. Once that is out there, move the original view to be a subview of the scroll view and set constraints to be 0 from the subview to the scroll view. Finally, right click drag from the File's Owner icon to the scroll view and set that as the view outlet.
Hopefully one of these will help you.
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..