I am trying to retrieve rect value of a class in ViewDidAppear. The button is in UITableViewCell. All values return correct except frame.origin.y. It returns -1.5. I am wondering what might cause that.
CGRect customRect = CGRectMake(self.favoriteButton.frame.origin.x, self.favoriteButton.frame.origin.y, self.favoriteButton.frame.size.height, self.favoriteButton.frame.size.width) ;
Origin.y is defined in an unintuitive.
First, moving down is positive and up is negative (aka larger numbers means the view will appear lower on the screen).
Second, the origin is relative to the top left corner of the immediate parent view (in your case the cell). If origin.y really is -1.5 then the top of your button is just one and a half points above the top of your table view cell, (which sounds likely). If your origin.y really should be 400-ish points it means for it to appear in your UITableViewCell your tableViewCell would have to be over 400 points tall.
This could be the case but I'm wondering if what you're looking for is not origin.y, but how far bellow the top of the root view (self.view in your view controller) the top of your button is. If so try:
CGPoint originInRootview = [self.view convertPoint:CGPointZero fromView:self.favoriteButton];
CGFloat theNumberIThinkYouWant = originInRootview.y
There are two things
1-) You said that it is in TableViewCell so it returns correct. Because you put button inside a view(TableViewVell container view), button gets it coordinates from container view not from superview.
2-) If TableViewCell is big enough and starts from y=0 and you are sure it is wrong and must be something like 400. Try to get the rect value at viewDidLayoutSubviews
All values return correct except frame.origin.y. It returns -1.5. I am wondering what might cause that.
Applying a transform to the view can cause it's frame property to be invalid. From the documentation for UIView's frame property:
Warning
When the value of this property is anything other than the identity
transform, the value in the frame property is undefined and should be
ignored.
So, if you're not seeing what you expect, compare your view's transform to CGAffineTransformIdentity.
Related
while using scrollview i have assigned contentSize (0,508)
if i change it to (-77,508) or (150,508) shows same result.
which result in myView top side doesnot comes down (how ever if i change the value of myView to Y = 250) it runs good.
But it doesnot look good in storyboard as myView bottom side goes way down the builder.
Any ideas, what i am doing wrong ?
contentSize property is responsible for (literally) the size of your content. Setting it to (150, 508) says "I want my content to be 150pt wide and 508pt tall". It defines how far side-to-side or top-to-bottom your content can scroll. It does not say anything about its actual position. If you want to change the position of the view inside the scroll view, either change the subview's frame or take a look at contentOffset property of the scroll view
I want to create a vertical UISlider and exactly fit it into an existing container view that is sized and placed from a .xib file using autoLayout. But I simply cannot get it to work and I have Googled my fingers bloody.
The closest I have gotten creates a slider that is too long and extends off the screen.
Am I putting the code in the wrong place?
I am putting my code in "layOutSubviews"
Some Points of interest:
1.Per Apple's docs, the view's "frame" is not valid after the transformation and should not be set.
2.The frame of the slider is set to the future parent's dimensions before the transformation (with x and y swapped).
3.The transformed view appears to maintain its bounds in the pre-transformation coordinate frame. i.e. After the 90 degree transformation, the width and height of the transformed view's bounds appear to be swapped.
This code doesn't work. The slider looks right except that it extends off the bottom of the screen. The slider bounds and even the frame (which Apple says is not valid) seem to match the bounds and frame of the container view. The slider doesn't even stay within its own bounds.
[super layoutSubviews];
CGRect trigLevelSliderFrame=self.trigLevelSliderContainer.bounds;
trigLevelSliderFrame.size.height=self.trigLevelSliderContainer.bounds.size.width;
trigLevelSliderFrame.size.width=self.trigLevelSliderContainer.bounds.size.height;
UISlider *mySlider=[[UISlider alloc] initWithFrame:trigLevelSliderFrame];
self.trigSlider=mySlider;
[mySlider release];
self.trigSlider.transform=CGAffineTransformMakeRotation(-M_PI_2);
CGPoint center=CGPointMake(self.trigLevelSliderContainer.bounds.size.width/2,self.trigLevelSliderContainer.bounds.size.height/2);
self.trigSlider.center=center;
[self.trigLevelSliderContainer addSubview:self.trigSlider];
There is no problem with this code to add and rotate the slider. The problem is that the the code must be put into the "layoutSubviews" method of the slider's parent view; instead I had it in the "layoutSubviews" of the parent view of the parent view of the slider. When my original code executed the slider's parent view had not yet been laid out and did not yet have the correct dimensions.
During "layoutSubviews" of a view, the subviews have not yet been laid out and so their bounds are not yet valid. I needed to wait until later in the layout process to get the bounds of the parent view and transform the slider to fit.
In the end, I put the code to add the slider and transform the slider in the "viewDidAppear" of the top level view controller. This was the same code as in the original question - just in a different place.
I'm trying to adjust the origin.y value of a UIView's frame. The problem that I'm running into is that the elements aren't moving with the frame with negative offsets. When I give the origin.y a positive value, it works perfectly: the view drops down, leaving black above, and the elements shift down as well. With a negative value, on the other hand, the view moves correctly, leaving black below, but the elements don't change position. I feel like the problem might be with autolayout, and the elements remaining trapped by the top of the screen, not the top of the UIView.
Any ideas on how to fix this?
I have a tablview that populates cells. When a cell is clicked I pragmatically populate textfields My question is how do I get the y coordinate so that I can position the UITextField based on where the user is currently located in the bounds? Or either make it so that I set the UITextField as a fixed position so that if you were to scroll down in the tableview the UITextField would always remain in the same place.
I don't mind either option but what would be the best approach to this?
UPDATE:
Okay here is what I am asking. When you populate a tablview with cells and it exceeds the screens physical seeing bounds you would scroll down to view the other cells right? So If i use:
float width = [[UIScreen mainScreen] bounds].size.width;
self.cardnameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, width, 25)];
0 = the y position. So that will populate at the top of my UiTableview at the 0,0 position which will be located at the top most left of the UITableView. Now what I am asking is lets say I scroll down to select a cell that exceeded the bounds of the physical screen and I selected the cell, it would populate the UITextField at the top most left of the UITableView 0,0 rather than the top most left of the physical screen. How do I get the physical screens y coordinate so I can position the UITextField so that the user will always see it.
Okay I just figured it out.
So when you execute [self.viewaddSubview:self.cardnameTextField]; this will position it within the UITableView which I would than have to find the y coordinate of the actual screens most top position. But if I run [self.view.window addSubview:self.cardnameTextField]; it sets it to the actual window giving it a almost CSS functionality of position:fixed; Which was exactly what I was looking for.
Even with your comment, it isn't very clear what you are asking.
If you are asking how to convert a cell's position to screen coordinates, it's pretty easy. UIView has a whole family of methods for converting coordinates from one view's coordinate system to another. They work as long as the 2 views are part of the same view hierarchy. You might take the cell's contentView.bounds.origin (which will be in the content view's coordinate system and) convert the position to the view controller content view's coordinate system:
CGPoint cellOrigin = cell.contentView.bounds.origin;
cellOrigin = [self.view convertPoint: cellOrigin fromView: cell.contentView];
The above code would give you the coordinates of a cell's origin, in the coordinates of the view controller's content view.
If that's NOT what you're asking then you will need to update your question to make it clear WHAT you are asking.
I am developing an iOS app and ran into a weird problem. I am doing a log of resizing of views and so on view did load, I have an ivar that holds the initial size of the view that I sized in the Interface Builder. The Initial CGFrame rect is: (x = 0, y = 63, width = 284, height = 705). The code I use to hold the initial value is:
filesFrame = self.fileView.frame;
and the only places i touch self.fileView.frame is:
self.fileView.frame = filesFrame;
self.fileView.frame = fullFilesFrame;
These are in two separate spots not right after each other
fullFilesFrame is defined as:
fullFilesFrame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y+63,
self.view.frame.size.height,self.view.frame.size.width+63);
When I check my values after I resize back to the initial frame, The frame rect is now origin CGPoint (x=0, y=63)
CGSize (width=28, height=961)
how can this be?
This is the first time I'm dealing with resizing of views, and Im fairly new to iOS as well.
I was going to put this in a comment, but it's too long, so:
One trick I like in these situations is to override setFrame: in the class of the view I'm having trouble with, and set a break point in that overridden method. If you do that then you can see when the frame changes and take a look at the call stack.
It's not always totally straight forward, overriding setFrame:. The frame property can be set directly but it can also be derived from the view's bounds and center, so you might need to look for things setting the view's bounds or the view's center instead of the frame.
Also if the view has a non-identity transform, that will make the frame property behave very strangely. If you are setting non-identity transforms on your view you should just avoid the frame property altogether.