Centering vertically in the viewcontroller with navigationbar in swift? - swift

I tried to center an UIImageView in my UIViewController which has a navigation bar at the top and what I wanna do is to have my UIImageView centered between the bottom of the navigationbar and the bottom of the view (with constraints of course). But Xcode make it vertically centered in the view from top of the view (centered between top of the navigationbar and bottom of the UIViewcontroller).
Is there a way to do what I wanna do? with constraints.

Add another UIView (empty) with constraints with top and bottom layout guide. Then add your UIImageView as a subview and align vertically and horizontally.

Try calling self.edgesForExtendedLayout = [] inside viewDidLoad()

Assuming your UIViewController in embedded in a UINavigationController then you should be able to make the center of your UIImageView equal to the center of the UIViewController's view. Otherwise if you added the nav bar manually you will need to offset the center Y coordinate of the UIImageView by the height of the nav bar.

Related

Content of NSScrollView sticks at bottom when resizing

When I add a custom NSView to my NSScrollView via documentView and resize the window the content sticks always to the bottom of the window (so the scroll view is always scrollt to the bottom when the window is resized).
Is there a way to keep the scroll view scrolled to the top when the window is resized?
Edit: Uploaded reproducible example:
https://github.com/nathasmike/sample1
The autoresizing mask of the custom view is translated into constraints. To prevent this do
myCustomView.translatesAutoresizingMaskIntoConstraints = false
myScrollView.documentView = myCustomView
Add constraints to the custom view and its subviews.
Another solution is removing the autoresizing mask from the custom view in the xib.

Why do these autolayout constraints not resize this UILabel?

In my project I have a UIScrollView view pinned to the edges of the screen. Inside that is a UIView pinned to the edges of the UIScrollView. Inside the UIView I have a UILabel constrained to the UIView so that there are 20 pixels to the left, right, and top of the superview. The problem is that the although the left and top constraints work the right one does not as the label is not correctly resized. I fell like there is something I fundamently don't understand about how constraints work.
my xcode console

My horizontal scrollview is not working

I've added a scrollview to a view in my app, the view is selected from a tab controller and as such is a container and not a UIViewController it is declared as below
class HomeViewController: Container, UIScrollViewDelegate {..
I've added the scrollview in the storyboard and added all of the components to it and I've assigned the delegate from the storyboard and I've placed this code in my ViewController
scrollview.contentSize = CGSize(width:1048, height:scrollview.frame.height)
scrollview.delegate = self
This how my ScrollView looks in my storyboard, you can see that scrollview is my UIScrollView, then I've added a UIView viewScroll and then added four views to viewScroll (View1, View2, View3 and View4), scrollview is sized at 375x340 and viewScroll is set at 1048x340
When I run the app it does not scroll. As it is not a UIViewController, do I need to approach this differently or is there something else I have missed?
I've added the scrollview in the storyboard and set the width as 1050 and height as 330
You should set the width and height of scroll view to be at most as large as its containing view. What you need to set to 1050 and 330 is contentSize of the scroll view - scrollView is only scrollable when its contentSize is larger than its bounds.
If you're setting up your scroll view using Interface Builder, then you'll need to add constraints to its children's edges. For example, in your case you'll have to add leading, trailing, top, and bottom constraints to your View Scroll view. Select it and add following constraints in interface builder:
After that there will be something like this:
When you add edge constraints to UIScrollViews child views, you are hinting the scroll view about its content size. And if scroll view knows its content size and it is larger than scroll view's bounds, you'll have scroll working.

How to make tableview's scroll bar scroll in a smaller rect than table's frame?

How to make tableview's scroll bar scroll in a smaller rect than table's frame?
Like this:A UITableViewController in a UINavigationController.
Tableview's frame is (0,0,320,480)(This frame said by NSLog), its scroll bar is always under 44px. So scroll bar will not displayed behind navigation bar.
How to archive this? Special thanks
You coud set scroll edge insets. For example:
UIScrollView *scrollView;
[scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(44, 0, 0, 0)];
As UITableView inherits UIScrollView all this methods are available.

Y-coordinate of UITabBar subview

I'm creating a custom subclass of a UIViewController (without a nib), which I'm pushing onto a UINavigationController stack. Somewhere during the initialization of my UIViewController subclass (loadView? viewDidLoad? init?) I want to add a UITabBar subview to the bottom of the view. The problem is figuring out the Y-coordinate. As far as I can tell, the view gets resized somewhere after loadView, viewDidLoad, and init so I can't get the resized height in order to calculate the Y-coordinate of the UITabBar.
What is the proper way to figure out the height of the containing view such that I can anchor the UITabBar at the bottom?
What you need to do is set your UITabBar to the bottom of the view, and then tell it to stay there if the bounds of the superview changes.
This isn't too tricky. For example, in viewDidLoad alloc/init the tab-bar as normal, and position it as follows:
tabBar.frame = CGRectMake(self.view.frame.size.height-tabBar.frame.size.height, 0,
tabBar.frame.size.height, tabBar.frame.size.height);
[self.view addSubview:tabBar];
...which will add the bar to the bottom of the view.
So far so good: your problem is when the superview changes height the tabbar doesn't stay locked to the bottom. To fix this, we set an autoresizing mask:
tabBar.autoresizingMask = UIViewAutoresizingMaskTopMargin;
...which will effectively lock the bar to the bottom of the superview.