iOS Swift : Keep UILabel constantly centered in UIView that changes height - swift

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.

Related

Resizing a UIView containing UIButtons makes those buttons lose perspective position

I got a UIView that contains an UIIMageView and several buttons that are allocated on certain position related to the image of the UIIMageView.
The frame of the UIView is (0,0,250,250)
the UIIMageView that is within the UIView is set to autorisizing (all) in IB.
All buttons are set to autoresize (none) and origin middle.
When i change the frame of the UIView i.e (0,0,500,500) the UIIMageView (and image) resize OK but the buttons lose their perspective position by a few pixels (about 2 to 3 in the vertical and horizontal axis).
Is this normal? can someone advise?
Try putting the buttons inside another UIView and then setting the autoresizing options on this new view. You should leave the buttons without autoresizing inside the new view.
there is no answer after all.
I must accept the fact that the resized view and the buttons within it will lose perspective position by 2/ 3 pixels off the x/y axis...
strange....

Scaling a sub classed UIView

I have created a subclass of UIView and am trying to scale and move the view from within its m file but am running into some problems. When I used the command:
self.frame = CGRectMake(self.frame.origin.x-10,self.frame.origin.y-10,self.frame.size.width/2,self.frame.size.height/2); the object moves location but does not resize (the view only contains a few UIImageViews). In the xib file of the sub class I have the options checked to Clip Subviews and to Autoresize Subviews but neither appears to happen. Any ideas as to why the view will not resize with this command and how I could get it to resize.
Resizing your view is not the same as scaling it. Think of your view as a picture frame. What you're doing above is moving the frame, and also moving the lower right corner (you're shortening the frame's wood bars) - but that does not automatically shrink the picture.
There are four ways of resizing a view with subviews:
Let the superview clips its subviews (by setting view.clipsToBounds = YES): Subviews do not resize or relayout, but only show in the area that is inside the frame.
Let the superview not clip its subviews (by setting view.clipsToBounds = NO): Changing superview size does not have any visual effect on subviews, they also show outside of the frame.
Give the subviews autoresizingMasks: The subviews do not change size, but they relayout according to their autoresizing mask (for instance, a subview may always stay 10 px off the lower right corner of the frame, or may always span exactly the width of the frame.) Note that this does not necessarily automatically scale subview content. Set subview.contentMode accordingly.
Scale the superview: By setting superview.transform = CGAffineTransformScale(superview.transform, 0.5, 0.5), you shrink the superview and all its subviews (you essentially zoom out). Note that this makes superview.frame undefined, which means you shouldn't use that anymore. It can also make things a bit blurry.
You could also "manually" change all the subviews, but that kind of defeats the purpose of having a nice view hierarchy.
As MishieMoo said, temporarily set the backgroundColor of your superview to something visible. This will very likely show you that your view is indeed changing.

Detect if a UILabel hide another UILabel on same UIView

I'm developing an iPhone application.
I have a UIView used to augmented reality. I add some UILabel to that UIView dynamically and I want to know if there is a way to know when a UILabel hide another UILabel added on same UIView.
In other words: I'm adding UILabels at the middle of the screen (y position is always the same, x can vary). When I'm going to add another UILabel, I want to know if there is another UILabel at the same position.
Note: A UILabel will have a (x,y) origin position and a height and a width.
Maybe there is a method to know where are "located" subview from a UIView.
Thanks.
If you do not apply any transforms to your views you can use CGRectIntersectsRect functions to views' frames:
if (CGRectIntersectsRect(label1.frame, label2.frame){
// Intersect
}

Drawing a shadow below a UIScrollView (that scrolls a UIView)

My setup is a UIScrollView in the center of the screen (on the iPhone - like 300x400 positioned in the center) that contains a UIView of the same width, so it scrolls it vertically. In this UIView i draw custom subviews with labels etc (it's a scoreboard with various colors).
What i'd like to have is some shadow below my UIScrollView, so that the whole scrolling scoreboard floats over my background.
I have found this nice post
How do I draw a shadow under a UIView?
I use this code in my ScrollView subclass but it doesn't work for me. Maybe because I don't draw the actual shapes in the ScrollView's drawRect: (since they are drawn on the UIView).
Also I guess that in order to have the View scroll in the ScrollView and the shadow of the ScrollView outside the scrolling area, I guess I should extend the "bounds" of the ScrollView, right?
It's not quite clear to me what you're asking but, if you want the scrollView contents to scroll over a static image you simply need to add a UIView (or more likely a UIImageView) to your superview and then add your UIScrollView to that. If you set he background colour of the UIScrollView to be celarColor, the background image will show through - so you have a view heirarchy like:
UIWindow
UIView <----- your background here
UIScrollView
Scrolling subviews <----- high score table here
If you draw your highscore table in the scrolling subviews using CoreGraphics, the answer in the question you linked to will also work.
How about explicitly filling the entire self.bounds rectangle in your scroll view subclass' drawRect: method before calling super?
Another idea is to put the scroll view inside of another view which does the shadow drawing.

iPhone UIScrollview: Button does not respond when located below 480 pixels on Scroll View's child UIView

I have built a view for an iPhone app in Interface Builder. It is a UIScrollview with a UIView and a UIButton on the UIView. The only code I have is setting the scroll view's contentSize to 320x550 in the viewDidLoad method of the xib's File Owner class. When the button is within the normal view area (320x480) the button responds as normal, but if is placed outside of those boundaries in Interface Builder the button will not respond when I scroll to it and click the button.
What am I missing? I figure it might be something I need to set on the UIView. But I am not sure what that is.
Your UIButton won't response even it's visible because it's not in the boundary of parent view. You can see object outside the boundary of its parent view because it's a default behavior of UIView to draw all subview (clipsToBounds = NO)
To see the truth, try this code.
UIView *yourUIView = ...
yourUIView.clipsToBounds = YES;
yourUIView.backgroundColor = [UIColor cyanColor];
You will no longer see your UIButton.
To fix this, enlarge your UIView.
I had the same problem as the person who posted the question. Thanks to the first answer, I had a hint as to what to do. I increased the height of the view by adjusting the frame. This, apparently must be done in code. Once this was done, however, the toolbar at the bottom was no longer visible. So before I adjust the height of the view, I grab the position of the tool bar. Then after adjusting the height of the view, I reset the position of the tool bar. Now all is good.