In the following function I define all the necessary characteristics of a UIView:
func OpenController() {
var gameOverView: UIView = UIView()
gameOverView.center = super.view.center
gameOverView.frame.size = CGSize(width: 200, height: 300)
gameOverView.backgroundColor = UIColor.grayColor()
self.view.addSubview(gameOverView)
}
Even though I define the center of the UIView "gameOverView" as that of the the viewcontroller it resides in, it appears with a corner in the center of the viewcontroller and not centered in the viewcontroller. I have tried various other ways of defining the position (NSLayoutConstraints, frame.x, frame.y etc.) but all have this result.
If anyone can tell me why this happens and how to center the UIView within its parent view controller I would greatly appreciate it!
Your issue here is that your center is being set before the frame. Since you are creating the view without the frame argument your frame is {0, 0}.
So you are currently centering the subview then resizing it, so this is happening:
What you need to do is resize the subview then center it, like this:
So you can just swap your centering and framing logic:
gameOverView.frame.size = CGSize(width: 200, height: 300)
gameOverView.center = super.view.center
Otherwise even easier just pass the frame when creating the view (you could even pass in the proper x, y coordinates to center it here too):
var gameOverView: UIView = UIView(frame: CGRectMake(0, 0, 200, 300))
You have to set the center of the view after setting its size. This is because before setting the size or frame of a view its frame rect is (x:0,y:0,width:0,height:0). If you then immediately set its center, then a view of size zero will get centered so its new frame rect could be (x:20,y:20,width:0,height:0) (if the parent view is 40x40). If you now change the view's size, the origin point of the view will not actually move so the new frame could be (x:20,y:20,with:5,height:5) which means the view is no longer centered.
So to center a view you have to first set its size and the its center.
Related
I have this simple code, initializing an UILabel
At first print, frame width/height are correct, but at second, all values of frame are 0.
let messageBoxFrame = CGRect(x: 0, y: 0, width: 1024, height: BAND_HEIGHT)
print(messageBoxFrame)
let messageBox = UILabel(frame: messageBoxFrame)
messageBox.textAlignment = .center
messageBox.textColor = UIColor.white
messageBox.backgroundColor = UIColor.blue
messageBox.sizeToFit()
print(messageBox.frame)
messageBox.sizeToFit() this line of code is adjusting your UILabel frame
As per this sizeToFit()
Call this method when you want to resize the current view so that it
uses the most appropriate amount of space. Specific UIKit views resize
themselves according to their own internal needs. In some cases, if a
view does not have a superview, it may size itself to the screen
bounds. Thus, if you want a given view to size itself to its parent
view, you should add it to the parent view before calling this method.
I am trying to create an interface that shows a floor plan with markers in fixed positions so when user zooms or scrolls the floor plan, markers stay on the same position relative to floor plan image.
So far I tried to do it with a UIScrollView that holds an UIImageView and then I tried to add some dummy subview
let view = UIView(frame: CGRect(origin: CGPoint(x: 400, y: 200), size: CGSize(width: 36, height: 36)))
view.backgroundColor = UIColor.redColor()
scrollView.addSubview(view)
but origin (in my example 400,200) is relative to screen, which means that the dummy subview shows only when you zoom enough and then slides along if you scroll around.
I can't figure out how to fix this. Maybe I should use some other UIKit classes?
Try that
create a container view of the preferred size
add the image view as a subview of this container view
add you dummy subview in the container view
from the zoom delegate method of the school view return the container view
The fact is that they need a common ancestor view
I'm trying to add a subview to a scrollview I have in my view controller:
let size:CGSize = self.view.bounds.size;
self.scrollview.contentSize.width = size.width
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, self.tableView.frame.origin.y + 130, size.width, size.height), pageMenuOptions: parameters)
self.scrollview.addSubview(pageMenu!.view)
It works to the extent that it adds it in the correct position and height I want it. But for some reason, right now it only expands to about 60% the width of the screen (I need it to be full screen).
Things I've tried
1) Setting it so self.view.frame.width
2) Setting it to the width of another full screen element.
3) Setting it to UIScreen.mainScreen().bounds
I checked the constraints of the scrollview in storyboard and it's configured to be full screen...so I'm not sure why this wont work.
This issue is related to the constraints that need to be set to the scroll view. I have answered a similar question here. Basically you need to specify a constraint for the scroll view's content view's width. See my answer in above link for a detailed description. The problem is that the scrollview adjusts its size to its content view's size even after we provide proper constraints to the scroll view. So we need to specify the constraints of the content view of the scroll view with respect to the scrollview and its superview so that the content in the scrollview fits our requirement.
One thing I noticed was that in your CGRectMake code you are specifying your y origin to be tableViews y value + 130. That seems like the problem to me.
I'm using a UIPresentationController, but I can't sort out how to get it showing the way I want it to.
I'm editing the frameOfPresentedViewInContainerView() function, and I need to be returning the frame (a CGRect) to display my content in.
The tutorial I followed uses CGRectInsert(self.containerView.bounds, 50, 50), which makes the window centered with the borders brought in 50px. If I return self.containerView.bounds just as itself, the view takes up the whole screen.
I'd like the overlaying view to be the width of the parent view (so, self.containerView.bounds.width), but I want the height to be the size needed to show the content of the new view without cutting anything off.
I tried a CGRect at (0,0) with width and height from self.preferredContentSize, but it's not returning sizes that work.. What can I do?
I tried frame = CGRect(x:0, y: self.containerView.bounds.height/2, width: self.containerView.bounds.width, height: self.containerView.bounds.height/2) just as a test (but that's just making the view half the size of the parent view), but when I rotate the screen, suddenly the new view is almost off screen..
Aside from frameOfPresentedViewInContainerView(), I also had to add the following to make it work.
- (void)containerViewWillLayoutSubviews
{
self.presentedView.frame = [self frameOfPresentedViewInContainerView];
}
Inside a UIViewController, I need to have the bottom half scrollable. So I added a UIScrollView and positioned it halfway down the view's height. And in the viewDidAppear method, I have put the below two code lines to make it scrollable.
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;
This way works if the scroll view fills the entire view, I've tested. But this method didn't work for my need. The scroll view would automatically move up and take up the entire screen. I assumed it was the second line of code which causes this.
So I removed the scroll view, added two UIViews to the view controller. To the bottom view, I added the UIScrollView. And in the viewDidAppear method, I have put the same two code lines changing the second line to refer the frame of the UIView that contains the scroll view..
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.containerView.frame;
But it wouldn't scroll either.
Can anyone please tell me how to do this correctly?
Thank you.
Dude, you keep setting the frame of the scrollView to something completely different from what you're actually trying to achieve.
If all you want to do is setup your scroll view so that it only occupies half the space then why dont you just set the frame so that the height only covers the portion of the screen that you want it to cover; and then set the x & y coordinates so that you draw the scroll view from the right position.
Do something like this:
//Shortcut to view's frame.
CGRect viewsFrame = self.view.frame;
/**
CGRectMake takes 4 parameters: x, y, width, height
x: is set to 0 since you want the scrollview to start from the left with no margin
y: you want the y position to start half way, so we grab the view's height and divide by 2
width: you want your scrollview to span from left to right, so simply grab the view's width
height: you want your scrollview's height to be half of your screen height, so get view's height and divide by 2.
*/
CGRect frameForSV = CGRectMake(0, viewsFrame.size.height/2, viewsFrame.size.width, viewsFrame.size.height/2);
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:frameForSV];
[self.view addSubview:myScrollView];
Then set your content size not based on an ansolute value, its best to have it based on the size of the content that's actually inside your scrollview so that your scrollview always scrolls to cover all your content inside it.
Also, remember that your scrollview will only scroll if the contentsize is greater than the scrollview's frame
UPDATE 1 after reading your comment in this post simply comment out any code in your viewController.m file related to your scrollview since youre setting up everything in interface builder.
This is the result: