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
Related
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.
I have a horizontal ScrollView of 20 (110 * 130 point) UIViews contained within a StackView. Each UIView contains an image.
When certain UIViews are hidden the UIView width is maintained at 110points. However, spaces are left were the hidden UIView should be.
I have been practicing with a StackView of 3 UIViews and hiding the center UIView. This leaves a space in the middle...
I would like the 3rd UIView to move into the 2nd UIView position (middle) and perform this for subsequent UIViews later on.
Does anyone know how I could go about doing this? Is this even possible?
I was hoping to accomplish this with a UICollectionView, however, I don't think you can drag an image from UICollectionView to UIView
Drag and Drop Image from UICollectionView to UIView?
A storyboard isn't meant for these complex user interactions, so I recommend you programmatically create your stack view and then set its constraints programmatically as well. This let's you change elements' heights in your code easily. This might help you get started:
Add views in UIStackView programmatically
I have a horizontal scrollview that fills 1/3rd of the view.
I want to put a UILabel in the centre of the scroll view, which I have successfully done with:
let reagentsLocationx = mainscrollview.frame.width/2-100
However, I am struggling to centre the label vertically within the scrollview. When I use similar code for reagentsLocationy it centres the label in the view and not within the scroll view.
How do I centre the label, in Swift, just within the scrollview?
Many thanks.
I think this is because you are using frame and not bounds. I don't believe frame will ever be larger than your screen space, but bounds it equal to the view's actual size. Try basing it on the bounds of the scroll views height.
UIScrollView().contentSize.height might be what you are looking for.
No need of setting sizes in code.
Everything done in the storyboard.
Drag your scrollview into your view controller.
Set top, bottom, leading and trailing constraints from the scrollview to the parent view or safe area.
Drag your text label into the scrollview.
Set your top, bottom, leading and trailing constraints to the scrollview.
IMPORTANT set a second leading and trailing constraint, now from the text label to the parent view (UIView - not scrollview) or safe area.
This makes sure that the text label knows his width and so the scrollview knows it too.
I have a UILabel centered within a UIView that expands/contracts depending on the device screen size. I've applied constraints so that the UILabel remains centered no matter the UIView size, which works fine.
Now I'm finding myself resizing the UIView manually like so (where mainView is the View Controller):
self.myView.frame = CGRectMake(0,0,self.mainView.frame.width, self.mainView.frame.height)
So this stretches the UIView to fill the whole screen/View Controller (it's also animated). I assumed the UILabel would continue to centre itself automatically, but it seems to pin itself as though it were constrained to the top of the UIView, leaving a lot of empty myView space below it.
How can I tell the UILabel to remain in the centre of the height-changing UIView that it's in?
We need to see your constraints to help, but:
You need to use centering constraints.
Set the bounds, not the frame of myView.
You might need to call setNeedsLayout on the view of the ViewController you are in
Also, set the background color of the UILabel -- it could be centered, but the text is not centered inside of it. For that, set the alignment properties.
I have a vertically-scrolling UITextView that fills the width of the screen. I need the text view to have margins (contentInset) of 20 pixels on the left and the right. But for some reason, I can't get the right-hand margin to work, either in Interface Builder or in XCode.
The reason I can't just make the text view narrower is because I am adding a subview which needs to run the full width of the screen. Also, I can't turn subview clipping off, because it plays havoc with a load of other elements on the screen.
Anyone know why the contentInset property is not affecting the right margin?
Thanks!
You could add an intermediate view that can be the superview to your text view and what was previously it's subview.
New View with Frame(0,0,width,height)
->TextView with Frame(20,0,width-20,height)
->Subview with Frame(0,0,width,height)