Is there a way to dynamically create a UIView with dynamically created UILabels inside it? I'd also like to center the UIView in the middle of the screen.
Any code would be appreciated.
I'm not entirely sure what you mean by "dynamically", but you can create a UIView and UILabels programmatically and use autoresizing masks to center them:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(20.f, 20.f, 300.f, 300.f)];
myView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoreszingFlexibleBottomMargin;
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(200.f, 200.f, 100.f, 100.f)];
myLabel.autoresizingMask = myView.autoresizingMask;
myLabel.text = #"My Label";
[myView addSubview:myLabel];
[myLabel release];
[self.view addSubview:myView];
[myView release];
Related
I want create some view, and add dynamically to it text and images
Somebody knows how I can to do it?
Thanks
Broad question, but this should get you on the right path: (Pop this into a viewcontroller)
-(void)addStuffToView{
UIView* myView = [self view]; // get the view controller's view
UIImageView* myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[myImageView setImage:[UIImage imageNamed:#"imagefile.jpg"]];
[myView addSubview:myImageView];
[myImageView release];
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,100,50)];
myLabel.text = #"Some text here";
[myView addSubview:myLabel];
[myLabel release];
}
Then, bind this method to your UIButton's touchUpInside event using Interface Builder.
I am doing an application where i am creating a UILabels programmitically,As i will be creating some 15 labels with different frames so i using only one method and calling it by parameter passing.
when i rotate it to Landscape view the position of the labels should be changed as there will lot of space left empty at right side..so i am trying to use Autoresizing mask but its not working,so please suggest what i should do so that label frame changes and fits at right position when i turn to landscape orientation. please provide me a sample code.The present code i am using is given below.
{
[self.view addSubview:[self createLabel:CGRectMake(450,140,60,20):#"Ratings:"]];
[self.view addSubview:[self createLabel:CGRectMake(450,170,60,20):#"Reviews:"]];
[self.view addSubview:[self createLabel:CGRectMake(50,170,50,20):#"Hours:"]];
[self.view addSubview:[self createLabel:CGRectMake(50,200,50,20):#"Phone:"]];
self.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
[super viewDidLoad];
}
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.textAlignment = UITextAlignmentLeft;
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = labelTitle;
[myLabel autorelease];
return myLabel;
}
You should set also autoresizingMask to all subview of self.view - add, for example, next line in the -(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle method:
myLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
or
myLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
I am using the following code to for Creating UILabels and calling it by paramater passing. I am using UIViewAutoSizingMask for autosize of labels when rotated in different orientation.
[self.view addSubview:[self createLabel:CGRectMake(450,240,60,20):#"Ratings:"]];
[self.view addSubview:[self createLabel:CGRectMake(450,270,60,20):#"Reviews:"]];
[self.view addSubview:[self Label:CGRectMake(505,240,60,20):aRequests.ratings]];
[self.view addSubview:[self Label:CGRectMake(510,270,60,20):aRequests.reviews]];`
self.view1.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.textAlignment = UITextAlignmentLeft;
myLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
myLabel.text = labelTitle;
[myLabel autorelease];
return myLabel;
}
In Portrait view it displays like this,
Ratings:3
Reviews:4
whereas, When rotated to landscape it display like this.
Ratings: 3
Reviews: 4
There is a space provided after "Ratings" Label. I have used all Autoresizing mask still its not working.
can anyone suggest anyone what is happening and what changes should be done in the above code.I tried all the autoresizeingMask everything but still i am not able to fix this.
The masks are conflicting eachother, use:
self.view1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
and
myLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.
I have set up an application and used some of the basic code from "The iPhone Developer's Cookbook" to create a simple view. The application works correctly in portrait mode, but when it is rotated, the dimensions of the boxes stay the same. Here is my code:
- (void)loadView {
//Create the full application frame (minus the status bar) - 768, 1004
CGRect fullRect = [[UIScreen mainScreen] applicationFrame];
//Create the frame of the view ie fullRect-(tabBar(49)+navBar(44)) - 768, 911
CGRect viewRect = CGRectMake(0, 64, fullRect.size.width, fullRect.size.height-93.0);
//create the content view to the size
contentView = [[UIView alloc] initWithFrame:viewRect];
contentView.backgroundColor = [UIColor whiteColor];
// Provide support for autorotation and resizing
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
[contentView release];
// reset the origin point for subviews. The new origin is 0,0
viewRect.origin = CGPointMake(0.0f, 0.0f);
// Add the subviews, each stepped by 32 pixels on each side
UIView *subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 32.0f, 32.0f)];
subview.backgroundColor = [UIColor lightGrayColor];
[contentView addSubview:subview];
[subview release];
subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 64.0f, 64.0f)];
subview.backgroundColor = [UIColor darkGrayColor];
[contentView addSubview:subview];
[subview release];
subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 96.0f, 96.0f)];
subview.backgroundColor = [UIColor blackColor];
[contentView addSubview:subview];
[subview release];
}
Is there any way to have this reload with the new dimensions when it is rotated or a better way of accommodating the orientation change?
Thanks
JP
I may be misunderstanding your problem. By "boxes", do you mean the subviews?
If so, the subviews will retain their apparent dimensions when rotated because they are squares.
In any case, viewDidLoad is only called when a view is first initialized, usually from a nib. If you need to make changes to a view when it rotates, you need to make the changes in willRotateToInterfaceOrientation:duration: and/or didRotateFromInterfaceOrientation:.
To change the dimensions of any view, simply reset its frame.
object.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;