Can't move a UILabel position - uilabel

I know, this is a stupid stupid question but after one day trying everything I've seen searching, I am uncapable of moving a UILabel, the code is this one:
CGRect frame = self.label_total.frame;
frame.origin.x = 500; // new x coordinate
frame.origin.y = 500; // new y coordinate
self.label_total.frame = frame;
it works if I put it in a IBAction, but I want to insert it in viewDidLoad, but It doesnt work. Please help me.

try this code little modified.
must provide outlet to label.
CGRect frame1 = lbl_total.frame ;
frame1.origin.x = 500; // new x coordinate
frame1.origin.y = 500; // new y coordinate
lbl_total.frame= frame1;

Related

Updating the width of NSTextField based on length of string

I want to make a label that change its size depending on the size of the string value that it will show. Currently I am doing this:
[tfScroll setStringValue:strScoller];
[tfScroll sizeToFit];
However this is not working. What am I missing?
If your tfScroll is NSTextField:
CGRect frame = tfScroll.frame;
frame.size.width = tfScroll.attributedStringValue.size.width+somepoints;//(somepoints=8)
tfScroll.frame = frame;
I did not find NSTextfield.contentSize.
If you trying to resize the fields's height, this may works:
CGRect frame = tfScroll.frame;
frame.size.height = tfScroll.contentSize.height;
tfScroll.frame = frame;

Change UIView layout programmatically for iPhone 5

I am trying to change the layout of my UIView programmatically, and not by using Auto Layout, as I am trying to deploy this app for iOS 4 and up. I basically set my view up with the screen height at 455(iPhone 5)(I know the actual screen size is 568 points, but I have a tab bar on the bottom so I am assuming the size shown in IB has taken that into account). I recorded the origin of all of my buttons and labels relative to the longer screen size and programmatically changed them to how I preferred them. The problem is, some of the buttons and labels are going off the screen now, so I think I have to perform some kind of conversion to keep them in bounds, but I am not sure what. See the pictures attached below:
Image before my initMethod is called to change button and label origins
Image after my initMethod is called to change the origin
View properties in XCode for the old screen size
This is how I want the view to Look. I moved the buttons around and recorded the origin of each.
Picture of my init method in viewWillAppear. Notice how the frame of the label is zero.
Here is the code I used:
-(void)initView
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 480) {
//iphone 4,4s
//done
}else if(screenBounds.size.height == 568) {
//iphone 5
CGRect frame;
//Static Professor label
frame = [staticProfessorLabel frame];
frame.origin.x = 48;
frame.origin.y = 218;
[staticProfessorLabel setFrame:frame];
//Professor Label
frame = [professorLabel frame];
frame.origin.x = 192;
frame.origin.y = 218;
[professorLabel setFrame:frame];
//show button
frame = [showProfessorButton frame];
frame.origin.x = 67;
frame.origin.y = 258;
[showProfessorButton setFrame:frame];
//clear proff button
frame = [clearProfessor frame];
frame.origin.x = 240;
frame.origin.y = 258;
//note label
frame = [bottomNote frame];
frame.origin.x = 160;
frame.origin.y = 310;
[bottomNote setFrame:frame];
//search button
frame = [searchButton frame];
frame.origin.x = 158;
frame.origin.y = 424;
[searchButton setFrame:frame];
//spinner
frame = [actIndicator frame];
frame.origin.x = 266;
frame.origin.y = 424;
[actIndicator setFrame:frame];
}
}
Also, I am calling this in the viewDidAppear method because when I call it in viewDidLoad, none of the buttons and labels give me a valid frame(I am guessing they are not initialized yet).
Your Xcode screenshots show that your storyboard contains constraints, so we can tell that your storyboard has autolayout turned on. If you want to set the view frames directly in code, you have to turn off autolayout.
Take a look at my answer here if you need help turning off autolayout on your storyboard.
Your hard-coded coordinates are simply wrong. Just double-check your values.
For example, bottomNote starts at x 160, which is clearly too far to the right.

How to center a grouped table view both vertically and horizontally on iOS?

I've been looking for a way to center a table view both vertically and horizontally but I can't seem to figure out if this is better done / possible using interface builder alone. If not what method should I override to set this in objective-c itself?
Also will this use the vertical screen size of the device so it actually centers the grouped table view on both legacy iPhone devices and the new iPhone 5?
Thank you in advance
This is what I use to get the proper centered frame. Note that the view needs to have proper springs and struts so it resizes when phone calls come in, etc:
+ (CGRect)centeredFrameForSize:(CGSize)size inRect:(CGRect)rect
{
CGRect frame;
frame.origin.x = rintf((rect.size.width - size.width)/2) + rect.origin.x;
frame.origin.y = rintf((rect.size.height - size.height)/2) + rect.origin.y;
// if(frame.origin.x < 0) frame.origin.x = 0;
// if(frame.origin.y < 0) frame.origin.y = 0;
frame.size = size;
return frame;
}

How do you change the width of a UITableView?

I'm new to iPhone development, coming from a web application development background and I'm working on my first project. I chose to create a Navigation based project, since after reading it seemed to be the easiest way to get what I'm after. How do you change the width of the UITableView? Eventually, I'd like to have a top bar, UITableView, and bottom bar be 304px wide. I've tried the following in my RootViewController's viewDidLoad method, but I'm doing it wrong:
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.size.width = 200;
self.tableView.frame = tableViewFrame;
Thanks for any help in advance.
If you want to change the width of the tableViewCell.. go to cellForRowAtIndexPath, and assign it there.
You could also do -
CGFloat tableBorderLeft = 1;
CGFloat tableBorderRight = 1;
CGRect tableRect = self.view.frame;
tableRect.origin.x += tableBorderLeft; // make the table begin a few pixels right from its origin
tableRect.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
tableView.frame = tableRect;
For changing the size of header
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return 150.0; //Give a value
}
For changing size of footer
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 150.0;//Give a value
}

Core Animation and Transformation like zoomRectToVisible

I'm trying to get an effect like the zoomRectToVisible-method of UIScrollview.
But my method should be able to center the particular rect in the layer while zooming and it should be able to re-adjust after the device orientation changed.
I'm trying to write a software like the marvel-comic app and need a view that presents each panel in a page.
For my implementation I'm using CALayer and Core Animation to get the desired effect with CATransform3D-transformations. My problem is, I'm not able to get the zoomed rect/panel centered.
the structure of my implementation looks like this: I have a subclass of UIScrollview with a UIView added as subview. The UIView contains the image/page in it's CALayer.contents and I use core animations to get the zooming and centering effect. The zoom effect on each panel works correcty but the centering is off. I'm not able to compute the correct translate-transformation for centering.
My code for the implementation of the effect is like this:
- (void) zoomToRect:(CGRect)rect animated:(BOOL)animated {
CGSize scrollViewSize = self.bounds.size;
// get the current panel boundingbox
CGRect panelboundingBox = CGPathGetBoundingBox([comicPage panelAtIndex:currentPanel]);
// compute zoomfactor depending on the longer dimension of the panelboundingBox size
CGFloat zoomFactor = (panelboundingBox.size.height > panelboundingBox.size.width) ? scrollViewSize.height/panelboundingBox.size.height : scrollViewSize.width/panelboundingBox.size.width;
CGFloat translateX = scrollViewSize.width/2 - (panelboundingBox.origin.x/2 + panelboundingBox.size.width/2);
CGFloat translateY = scrollViewSize.height/2 - (panelboundingBox.size.height/2 - panelboundingBox.origin.y);
// move anchorPoint to panelboundingBox center
CGPoint anchor = CGPointMake(1/contentViewLayer.bounds.size.width * (panelboundingBox.origin.x + panelboundingBox.size.width/2), 1/contentViewLayer.bounds.size.height * (contentViewLayer.bounds.size.height - (panelboundingBox.origin.y + panelboundingBox.size.height/2)));
// create the nessesary transformations
CATransform3D translateMatrix = CATransform3DMakeTranslation(translateX, -translateY, 1);
CATransform3D scaleMatrix = CATransform3DMakeScale(zoomFactor, zoomFactor, 1);
// create respective core animation for transformation
CABasicAnimation *zoomAnimation = [CABasicAnimation animationWithKeyPath:#"transform"];
zoomAnimation.fromValue = (id) [NSValue valueWithCATransform3D:contentViewLayer.transform];
zoomAnimation.toValue = (id) [NSValue valueWithCATransform3D:CATransform3DConcat(scaleMatrix, translateMatrix)];
zoomAnimation.removedOnCompletion = YES;
zoomAnimation.duration = duration;
// create respective core animation for anchorpoint movement
CABasicAnimation *anchorAnimatione = [CABasicAnimation animationWithKeyPath:#"anchorPoint"];
anchorAnimatione.fromValue = (id)[NSValue valueWithCGPoint:contentViewLayer.anchorPoint];
anchorAnimatione.toValue = (id) [NSValue valueWithCGPoint:anchor];
anchorAnimatione.removedOnCompletion = YES;
anchorAnimatione.duration = duration;
// put them into an animation group
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = [NSArray arrayWithObjects:zoomAnimation, anchorAnimatione, nil] ;
/////////////
NSLog(#"scrollViewBounds (w = %f, h = %f)", self.frame.size.width, self.frame.size.height);
NSLog(#"panelBounds (x = %f, y = %f, w = %f, h = %f)", panelboundingBox.origin.x, panelboundingBox.origin.y, panelboundingBox.size.width, panelboundingBox.size.height);
NSLog(#"zoomfactor: %f", zoomFactor);
NSLog(#"translateX: %f, translateY: %f", translateX, translateY);
NSLog(#"anchorPoint (x = %f, y = %f)", anchor.x, anchor.y);
/////////////
// add animation group to layer
[contentViewLayer addAnimation:group forKey:#"zoomAnimation"];
// trigger respective animations
contentViewLayer.anchorPoint = anchor;
contentViewLayer.transform = CATransform3DConcat(scaleMatrix, translateMatrix);
}
So the view requires the following points:
it should be able to zoom and center a rect/panel of the layer/view depending on the current device orientation. (zoomRectToVisible of UIScrollview does not center the rect)
if nessesary (either device orientation changed or panel requires rotation) the zoomed panel/rect should be able to rotate
the duration of the animation is depending on user preference. (I don't know whether I can change the default animation duration of zoomRectToVisible of UIScrollView ?)
Those points are the reason why I overwrite the zoomRectToVisible-method of UIScrollView.
So I have to know how I can correctly compute the translation parameters for the transformation.
I hope someone can guide me to get the correct parameters.
Just skimmed over your code and this line is probably not being calculated as you think:
CGPoint anchor = CGPointMake(1/contentViewLayer.bounds.size.width * (panelboundingBox.origin.x + panelboundingBox.size.width/2), 1/contentViewLayer.bounds.size.height * (contentViewLayer.bounds.size.height - (panelboundingBox.origin.y + panelboundingBox.size.height/2)));
You're likely to get 0 because of the 1/ at the start. C will do your multiplication before this division, resulting in values <1 - probably not what you're after. See this
You might find it more useful to breakdown your calculation so you know it's working in the right order (just use some temporary variables) - believe me it will help enormously in making your code easier to read (and debug) later. Or you could just use more brackets...
Hope this helps.