Centering UI elements in middle of screen programmatically - ios

I'm trying to do something fairly simple, but just can't manage to make it happen. I have an UIImage followed by two UILabels, each on on top of the other, that I want to center in the middle of a UIView.
(empty space)
UIImage
UILabel
UILabel
(empty space)
This is what I have so far:
UIView* baseView = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,[[UIScreen mainScreen] applicationFrame].size.height)];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
[imageView setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[label1 setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[label2 setCenter:CGPointMake(baseView.frame.size.width / 2, baseView.frame.size.height / 2)];
[baseView addSubview:imageView];
[baseView addSubview:label1];
[baseView addSubview:label2];
[self addSubview:baseView];
But it doesn't seem to be working. All the elements are over each other. Any ideas?

Have it a try.
I add an container view to hold the imageView and the two labels, then center the container view in baseView. Set backgroundColor of container to clear color so we can't see it.
UIView* baseView = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] applicationFrame].size.width,[[UIScreen mainScreen] applicationFrame].size.height)];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 175, 175)];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 175+50+50)] ;
container.backgroundColor = [UIColor clearColor] ;
[imageView setCenter:CGPointMake(container.bounds.size.width/2, imageView.bounds.size.height/2)];
[label1 setCenter:CGPointMake(imageView.center.x, imageView.bounds.size.height + label1.bounds.size.height / 2)];
[label2 setCenter:CGPointMake(imageView.center.x, label1.frame.origin.y + label1.bounds.size.height + label2.bounds.size.height / 2)];
container.center = CGPointMake(baseView.bounds.size.width/2, baseView.bounds.size.height/2) ;
[container addSubview:imageView];
[container addSubview:label1];
[container addSubview:label2];
[baseView addSubview:container] ;
[self addSubview:baseView];

Related

How to increase the HMSegmentedControl count?

hello I am using HMSegmentedControl for swipe tab feature. And my code is like this.
// Tying up the segmented control to a scroll view
self.segmentedControl4 = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 260, viewWidth, 50)];
self.segmentedControl4.sectionTitles = #[#"Worldwide", #"Local", #"Headlines",#"Four",#"Five"];
self.segmentedControl4.selectedSegmentIndex = 1;
self.segmentedControl4.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.segmentedControl4.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
self.segmentedControl4.selectedTitleTextAttributes = #{NSForegroundColorAttributeName : [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1]};
self.segmentedControl4.selectionIndicatorColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
self.segmentedControl4.selectionStyle = HMSegmentedControlSelectionStyleBox;
self.segmentedControl4.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationUp;
self.segmentedControl4.tag = 3;
__weak typeof(self) weakSelf = self;
[self.segmentedControl4 setIndexChangeBlock:^(NSInteger index) {
[weakSelf.scrollView scrollRectToVisible:CGRectMake(viewWidth * index, 0, viewWidth, 200) animated:YES];
}];
[self.view addSubview:self.segmentedControl4];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 310, viewWidth, 210)];
self.scrollView.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(viewWidth * 5, 200);
self.scrollView.delegate = self;
[self.scrollView scrollRectToVisible:CGRectMake(viewWidth, 0, viewWidth, 200) animated:NO];
[self.view addSubview:self.scrollView];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 210)];
[self setApperanceForLabel:label1];
label1.text = #"Worldwide";
[self.scrollView addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth, 0, viewWidth, 210)];
[self setApperanceForLabel:label2];
label2.text = #"Local";
[self.scrollView addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label3];
label3.text = #"Headlines";
[self.scrollView addSubview:label3];
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label4];
label4.text = #"Four";
[self.scrollView addSubview:label4];
UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth * 2, 0, viewWidth, 210)];
[self setApperanceForLabel:label5];
label5.text = #"Five";
[self.scrollView addSubview:label5];
My problem is it doesnt show these label4 and label 5 only show upto 3. But all 5 titles show from the section titles array. How can I solve these problem.
Please help me.
ThanksThis is the github project I used
I don't know about HMSegmentedControl but i use Page Menu for scroll pages like segment controller. you can add your view controller directly into this menu segment.
I am not sure that this is a perfect answer of this question but it might help others as well.
Set your self.segmentedControl4.tag = 5;
Give tag numbers which you want to show in segmentedControl.

Unable to center a imageView in a tableView Footer

I am trying to center a label and an image in a tableView Footer
I can't seem to make it work for both device iPhone and iPad
Right now it is centered on the iPhone but that's because I hardcoded it.
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
[view setBackgroundColor:[UIColor clearColor]];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, self.view.bounds.size.width, 20)];
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
[lbl setText:#"Powered By"];
[lbl setFont:[UIFont systemFontOfSize:10]];
[lbl setTextAlignment:NSTextAlignmentCenter];
[lbl setTextColor:[UIColor blackColor]];
District *school = [District new];
UIImageView * logoView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 25, 150, 37.5)];
logoView.image = [UIImage imageNamed:#"Logo.png"];
[logoView autoresizingMask];
[view addSubview:logoView];
[view addSubview:lbl];
return view;
}
I would like to center this view, without hardcoding it. I tried taking the screen size and divide by 2.
It did not center, what is the right approach, please advise.
You need to set the view's autoresizingMask as needed.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
[view setBackgroundColor:[UIColor clearColor]];
CGRect lblFrame = view.bounds;
lblFrame.origin.y = 15;
lblFrame.size.height = 20;
UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame];
lbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
lbl.lineBreakMode = NSLineBreakByWordWrapping;
lbl.numberOfLines = 0;
[lbl setText:#"Powered By"];
[lbl setFont:[UIFont systemFontOfSize:10]];
[lbl setTextAlignment:NSTextAlignmentCenter];
[lbl setTextColor:[UIColor blackColor]];
District *school = [District new];
CGRect logoFrame = CGRectMake((view.bounds.size.width - 150) / 2.0, 25, 150, 37.5);
UIImageView * logoView = [[UIImageView alloc]initWithFrame:logoFrame];
logoView.image = [UIImage imageNamed:#"Logo.png"];
logoView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[view addSubview:logoView];
[view addSubview:lbl];
return view;
}
This assumes the label should fill the width and the image should stay centered left-to-right.

Why is nothing displaying in my UIView?

I have following code that adds a UIView and then adds a UIImageView and UILabel, the UIView display correctly but nothing i have put in side the view is showing. I have looked everywhere but i cannot find an answer. The UIView is presented under certain conditions and when it is tapped it dissapears.
Here is my code.
UIView *actionView = [[UIView alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[actionView addGestureRecognizer:singleFingerTap];
actionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
actionView.layer.cornerRadius = 5;
actionView.layer.masksToBounds = YES;
[self.view addSubview:actionView];
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
actionText.Frame = CGRectMake(20, 180, 280, 165);
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109, 273, 40, 40);
[actionView addSubview:blockView];
Change your frame positions like..
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109,70, 40, 40);
[actionView addSubview:blockView];
When you set the frame of a view, it is important to remember that the origin relates to the coordinate system of the superview it is added to.
So when you set the frame of your label to:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
the origin of x = 20, y = 180 end up being the origin WITHIN the actionView. Now this is your problem because your actionView is only 165px tall, and you are telling the actionText subView that it's y origin is 180, which is well outside the bottom of the actionView.
for your actionText, you should allocate it as follows:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 165)];
and for your blockView, something like:
blockView.frame = CGRectMake(89,93, 40, 40);

Gap is existing when customize the navigationbar with imageView

Im using the following codes to customize the navigationbar with an UIView. But why there is a gap in the left size of the navigationbar? ps. the gradient image is test.png.
Thanks in advance!
- (void)viewDidLoad
{
[super viewDidLoad];
// show image
UIImage *image = [UIImage imageNamed: #"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = #"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}
Edit 1
Here is my test code and screen shot
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;
[self.view addSubview:viewGreen];
}
your code is work fine.just check the image and transparency.
To add the line at last and check again
[self.view addSubview:newView];
problem is solved by the following code
[self.navigationBar insertSubview:imageView atIndex:1];

How do I add Two UILabels in my UIScrollView for the iphone?

myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
CGSize scrollViewContentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+500);
[myscrollview setContentSize:scrollViewContentSize];
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
But I don't know My Firstmasg length.... after that lblfirstmsg I want to add second lblsecongmgs.... So how can I add?
After you set first label and set it content you can call [lblfirstmsg sizeToFit]; to fit frame to its content. Then you can add your second UIlabel next to the first one [[UILabel alloc] initWithFrame:CGRectMake(lblfirstmsg.frame.origin.x + lblfirstmsg.frame.size.width, lblfirstmsg.frame.origin.y, 200, 50)];
[myscrollview addSUbview:lblluckyno];
and same way add 2nd label.
This is your scroll view code.
myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
myscrollview.delegate = self;
[self.view addSubview:myscrollview];
[myscrollview setContentSize:CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+500);];
After you set two label With CGRect and add in Scroll View
[myScrollView addSubView:lblluckyno];
......
Hello Mayur P Bhansali,
You can find the position and dimension of your first label like so:
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
UILabel *secondLabel = [[UILabel alloc] initWithFrame: CGRectMake(lblluckyno.frame.origin.x + lblluckyno.frame.size.width, 200, 100)];
Each UIView has a "frame" object and this frame object has a "origin" C structure and "size" C structure. Using labelName.frame.size.width gives you the width of a frame and labelName.frame.origin.x gives you the X coordinate of the frame.
You could access these properties like this too:
[labelName frame].size.width
[labelName frame].size.origin.x
lblluckyno = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 200, 50)];
lblfirstmsg = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 295, 800)];
[myscrollview addSUbview:lblluckyno];
[myscrollview addSUbview:lblfirstmsg];

Resources