Need help properly positioning UIView in a UIView - iphone

I made a UIView subclass (bowl) and when I try to make a bowl within a bowl and then center the inner one on the outer one's center it end up getting placed below and to the right of the center. How would I properly center it?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0].CGColor;
self.userInteractionEnabled = YES;
UIGestureRecognizer *twoFingerZ = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchZoom:)] ;
[self addGestureRecognizer:twoFingerZ];
UILabel *label = [[UILabel alloc] init];
[label setFont:[UIFont fontWithName:#"Arial" size:64]];
label.text = [NSString stringWithFormat:#"March"];
[label sizeToFit];
label.center = self.center;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor lightGrayColor];
self.backgroundColor = [UIColor whiteColor];
[self addSubview:label];
}
return self;
}
-(void)pinchZoom:(UIPinchGestureRecognizer *)recognizer
{
if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateEnded) {
CGAffineTransform newForm = CGAffineTransformMakeScale(600/self.bounds.size.width, 600/self.bounds.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[self setTransform:newForm];
return;
[UIView commitAnimations];
}
if([(UIPinchGestureRecognizer*)recognizer state] == UIGestureRecognizerStateBegan) {
subClip = [[clipView alloc] initWithFrame:CGRectMake((self.center.x - ([delegate getSize:self].width/2)), (self.center.y - ([delegate getSize:self].height/2)), [delegate getSize:self].width, [delegate getSize:self].height)];
subClip.transform = CGAffineTransformMakeScale(0.1, 0.1);
[self addSubview:subClip];
bowl *newB = [[bowl alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
newB.center = self.center;
// right here! *************
[self addSubview:newB];
}
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
EDIT: I fixed it. Apparently the center value for a UIView isn't exactly what I thought it was (it looks like it's the position of the center in another view or something), so instead of newB.center = self.center, the right way to do it was newB.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

To center the inner label you should be able to use:
label.center=CGPointMake(self.center.x,self.center.y);
But first you'll probably want to define the frame size of the inner label:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,yourWidth,yourHeight);
You may also want to center the text within the inner label:
label.textAlignment = UITextAlignmentCenter;
Also, not sure what you're intending in your code with this:
[label sizeToFit];
But according to the documentation, sizeToFit will resize and moves the receiver view so it encloses its subviews. In this case, it doesn't appear that your inner view has subviews. So your centering may be off because you may have unexpected dimensions for your inner label. Try setting the background color for that label to something you can see (i.e. not [UIColor clearColor]) to double check its dimensions. May help solve the centering problem.
Good luck.

Related

Add fixed UILabel in UIPickerView

I saw a this question and answer and I tried a few options but non worked.
I would like to create a UIPickerView like the one below, (fixed labels inches and feet) but those wouldn't appear:
I create the UIImagePicker like this:
- (void)viewDidLoad
{
_picker = [[UIPickerView alloc] init];
CGRect pickerFrame = CGRectMake(0, 0, 200, 216);
pickerView.frame = pickerFrame;
pickerView.userInteractionEnabled = YES;
pickerView.dataSource = self;
pickerView.hidden = YES;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
[textField setInputView:pickerView];
textField.delegate = self;
[pickerView removeFromSuperview];
_picker.hidden = YES;
}
- (BOOL) textFieldShouldBeginEditing:(UITextView *)textView
{
if (textView.tag==1){ //field for the uipickerview
_picker.hidden = NO;
[self addPickerLabel:#"Feet" rightX:114 top:342 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:342 height:21];
}
return YES;
}
- (void)addPickerLabel:(NSString *)labelString rightX:(CGFloat)rightX top:(CGFloat)top height:(CGFloat)height {
#define PICKER_LABEL_FONT_SIZE 18
#define PICKER_LABEL_ALPHA 0.7
UIFont *font = [UIFont boldSystemFontOfSize:PICKER_LABEL_FONT_SIZE];
CGFloat x = rightX - [labelString sizeWithFont:font].width;
// White label 1 pixel below, to simulate embossing.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, top + 1, rightX, height)];
label.text = labelString;
label.font = font;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.alpha = PICKER_LABEL_ALPHA;
[_picker addSubview:label];
// Actual label.
label = [[UILabel alloc] initWithFrame:CGRectMake(x, top, rightX, height)];
label.text = labelString;
label.font = font;
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
label.alpha = PICKER_LABEL_ALPHA;
[_picker addSubview:label];
}
The picker appears, but without the fixed labels of inches and feet.
What is wrong?
Move this lines to viewDidLoad and try it.Labels need to be added
once.Not always when textfield did begin editing
[self addPickerLabel:#"Feet" rightX:114 top:342 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:342 height:21];
Log the frame of both the label and set it correct if it appears wrong
NSLog(#"%#",NSStringFromCGRect(label.frame));
Your UIPickerView has height of 216, but you put the labels at height 342. This might be the reason you can't see them.
Edit:
Try replacing the lines where you make the labels to
[self addPickerLabel:#"Feet" rightX:114 top:98 height:21];
[self addPickerLabel:#"Inches" rightX:241 top:98 height:21];
I used the already implemented great component:
https://github.com/brunow/ActionSheetPicker2
Which provides a multicolumn picker view and I simply changed the text and the amount of columns

Getting tag of UILabel on click

I am dynamically creating UILabels and then saving their tag in an NSMutableArray. I then have a method that detects taps (clicks) on these UILabels. Basically when a UILabel that has been dynamically generated is clicked I want to have it deleted without deleting other labels. However, in future I may want to do more then just delete. But at the moment I feel like I am stuck at a dead end trying to find a way to do this. Any ideas?
Heres my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// set corner radius
coverview.hidden=YES;
labeltextfield.hidden=YES;
textcreate.hidden=YES;
labeltags = [NSMutableArray array];
labeltext = [NSMutableArray array];
}
-(IBAction)removeboard
{
[labeltextfield resignFirstResponder];
}
-(void)showtextcreator {
// Create bg cover
coverview.hidden=NO;
labeltextfield.hidden=NO;
textcreate.hidden=NO;
//Make sure creating screen is always on top
[self.view bringSubviewToFront:coverview];
[self.view bringSubviewToFront:labeltextfield];
[self.view bringSubviewToFront:textcreate];
}
-(void)createtext {
NSInteger obj = [labeltags count] +1 ;
[labeltags addObject:[NSNumber numberWithInteger:0]];
int posx = arc4random() % 300 ;
int posy = arc4random() % 400 ;
int frame = arc4random() % 400 ;
NSString *txt = labeltextfield.text;
// NSString *framename = (#"frame%i",frame);
[labeltext addObject:txt];
[labeltags addObject:[NSNumber numberWithInteger:0]];
CGRect labelframe = CGRectMake( posx, posy, 100, 30);
label = [[UILabel alloc] initWithFrame: labelframe];
[label setText: [NSString stringWithFormat:#"%#", txt]];
[label setTextColor: [UIColor orangeColor]];
label.backgroundColor = [UIColor clearColor];
label.tag=obj;
[self.view addSubview: label];
label.userInteractionEnabled = YES;
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(labelDragged:)];
[label addGestureRecognizer:gesture];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction)];
[label addGestureRecognizer:recognizer];
coverview.hidden=YES;
labeltextfield.hidden=YES;
textcreate.hidden=YES;
}
- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];
// move label
label.center = CGPointMake(label.center.x + translation.x,
label.center.y + translation.y);
// reset translation
[gesture setTranslation:CGPointZero inView:label];
}
- (void)tapAction {
UILabel *labelnew = (UILabel *)[self.view viewWithTag:1];
NSLog(#"Text is %#",labelnew.text);
}
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)];
[label addGestureRecognizer:recognizer];
- (void)tapAction:(UITapGestureRecognizer *)tapGesture {
UILabel *labelTapped = (UILabel *)tapGesture.view;
//delete it using removeFromSuperView or do whatever you need with tapped label
}
Details:
1.Modify your -(void)createtext method.
2: Add a parameter to the target for UITapGestureRecognizer
3.Receive the sender gesture in - (void)tapAction:
4.Get the tapped UILabel.
Thats it.

Why the UILabel is not displaying?

In my appDelegate.m I have this code, but the UILabel is not displaying, but only the UIImageView.
Why?
notificationView = [[UIImageView alloc] initWithFrame: CGRectMake(154, 320, 140, 47)];
notificationView.image = [UIImage imageNamed:#"notification.png"];
NSString *message = [NSString stringWithString:#"New Videos"];
notificationLabel = [[UILabel alloc] initWithFrame: CGRectMake(165, 313, 140, 47)];
notificationLabel.font = [UIFont boldSystemFontOfSize:12];
notificationLabel.textAlignment = UITextAlignmentLeft;
notificationLabel.text = [NSString stringWithFormat:#"%d %#", numberOfVideos, message];
notificationLabel.textColor = [UIColor whiteColor];
notificationLabel.backgroundColor = [UIColor clearColor];
notificationView.alpha = 0.0;
notificationLabel.alpha = 0.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
notificationView.alpha = 1.0;
notificationLabel.alpha = 1.0;
[UIView commitAnimations];
[self.window addSubview: notificationView];
[self.notificationView addSubview: notificationLabel];
[UIView commitAnimations];
[notificationView release];
[notificationLabel release];
notificationLabel is a subview of notificationView.
It is placed out of bounds of its superview.
Try
notificationLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 140, 47)];
and go from there. For all subviews, the top left corner of a super view is (0, 0).
The width of the notification view is 140. Your label is being placed at an X location of 165 within the notification view, which is out of the bounds of the notification view. So it isn't visible.

Keyboard blocking my view

I have a UIView with another UIView inside it. On the inside UIView there is a textbox which I want to fill in. When I try to fill it in the keyboard blocks my view:
The UIViewController has the following
containerView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view=containerView;
//The apropriate releases etc are further on...
When I touch it, the keyboard comes up as expected, but blocks the textbox I'm trying to fill in. How can I force the view to slide up?
The front view
OptionsFront * fv = [[OptionsFront alloc] initWithFrame:[UIScreen mainScreen].bounds];
[containerView addSubview:frontView];
In the front view is a subview
CGRect bounds = CGRectMake(0.0f, 210.0f, 280.0f, 130.0f);
sv = [[UIView alloc] initWithFrame:bounds];
[self addSubview:sv]; //added to frontView
In sv is a textbox near the botton:
rect = CGRectMake(70.0f, 20.0f, 100.0f, 27.0f);
cf = [self createTextField_Rounded:rect holder:#"+ve"];
[sv addSubview:cf];
So cf happens to be near the bottom of the page. I expected that when I select it, the whole display would move up, but the keyboard just moves up and blocks it.
What can I do?
Appendix:
- (UITextField *)createTextField_Rounded:(CGRect) frame holder:(NSString *) ph
{
UITextField *returnTextField = [[UITextField alloc] initWithFrame:frame];
returnTextField.borderStyle = UITextBorderStyleRoundedRect;
returnTextField.textColor = [UIColor blackColor];
returnTextField.font = [UIFont systemFontOfSize:17.0];
returnTextField.delegate = self;
returnTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
returnTextField.returnKeyType = UIReturnKeyDone;
returnTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
return returnTextField;
}
you need to move the textField up. in your UITextField delegate implement something like (from memory):
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
textField.superview.frame = CGRectMake(0.0f, 100.0f, 280.0f, 130.0f); // experiment with frame size
[UIView commitAnimations];
}
and you'll want to move it back in textFieldDidEndEditing:

iphone: rotating flipped view when rotating visible view

I have a resizing issue when rotating a view controller that has two views which I switch between using the flip animation.
The problem appears if i do the following steps:
Rotate the device when viewing the tableview.
Click the info button.
Rotate the device (infoView appears stretched).
Click on info button (tableview appears stretched)
It appears that the view that is not added to the superview is not resizing correctly, since it was not part of the composite views when the device was rotated.. Is there any way to get this view auto resized correctly?
Below is a code sample
- (void)viewDidLoad {
[super viewDidLoad];
//background image
backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg-app.png"]];
backgroundImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
[self.view addSubview: backgroundImageView];
[backgroundImageView release];
//infoView
aboutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"About.png"]];
aboutImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
//tableView
self.tableView = [[[UITableView alloc ] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease ];
//set the rowHeight once for performance reasons.
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = 75;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.autoresizesSubviews = YES;
//set the rowHeight once for performance reasons.
[self.view addSubview: tableView];
//[tableView release];
[self.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];//for resizing on rotation
//info button
UIButton * infoDarkButtonType = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoDarkButtonType.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
infoDarkButtonType.backgroundColor = [UIColor clearColor];
[infoDarkButtonType addTarget:self action:#selector(infoAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonInfo = [[UIBarButtonItem alloc] initWithCustomView:infoDarkButtonType];
self.navigationItem.rightBarButtonItem = buttonInfo;
[infoDarkButtonType release];
self.navigationItem.rightBarButtonItem = buttonInfo;
[buttonInfo release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
- (void)infoAction:(id)sender{
NSLog(#"Clicked on the info button");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75]; /* Sub. duration here */
UIView *superview;
if ((superview = [tableView superview])) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:superview cache:YES];
[tableView removeFromSuperview];
[superview addSubview:aboutImageView];
} else if ((superview = [aboutImageView superview])) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:superview cache:YES];
[aboutImageView removeFromSuperview];
[superview addSubview:tableView];
}
[UIView commitAnimations];
}
Thanks
Try putting the code in viewWillAppear:animated. It's called every time your view will appear. View did load is called once.