How to autoresize UIScrollView - ios

In my app is an infopage. The textllength could differ due to localization. Beside that has a 3.5" screen less real-estate than a 4" display. So what I try to achieve is that the UILabel "grows" inside a ScrollView and the Scrollview adapts to the screen.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create a ScrollView and a label
UIScrollView *infoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 55.0f, 300.0f, 400.0f)];
CGRect labelFrame = CGRectMake(0, 0, 280, 800);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:labelFrame];
NSString *labelText = NSLocalizedString (#"InfoText",#"");
[infoLabel setText:labelText];
// Tell the label to use an unlimited number of lines
[infoLabel setNumberOfLines:0];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont boldSystemFontOfSize:17]];
infoLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
[infoLabel sizeToFit];
infoScroll.contentSize = CGSizeMake(infoScroll.contentSize.width, infoLabel.frame.size.height);
[infoScroll addSubview:infoLabel];
[self.view addSubview:infoScroll];
}
Please advice, thanks in advance

Modify your function by below way:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Create a ScrollView and a label
UIScrollView *infoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 55.0f, self.view.frame.size.width - 20.0, self.view.frame.size.height - 55)];
infoScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectZero];
infoLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *labelText = NSLocalizedString (#"InfoText",#"");
[infoLabel setText:labelText];
// Tell the label to use an unlimited number of lines
[infoLabel setNumberOfLines:0];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont boldSystemFontOfSize:17]];
infoLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
//[infoLabel sizeToFit];
CGSize infoLabelSize = [infoLabel.text sizeWithFont:infoLabel.font
constrainedToSize:CGSizeMake(infoScroll.frame.size.width, 5000)
lineBreakMode:UILineBreakModeWordWrap];
infoLabel.frame = CGRectMake(0, 0, infoLabelSize.width, infoLabelSize.height);
infoScroll.contentSize = infoLabelSize;
[infoScroll addSubview:infoLabel];
[self.view addSubview:infoScroll];
}

Related

How can we insert multiple titles on UInavigationBar in ios

Hi i am beginner in iOS and in my project i need to insert "titles" at "left side center" and "right side center" on UInavigationBar as like below image please help me how should i do below my requirement
See below code and try :
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)];
lbl1.backgroundColor = [UIColor redColor];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.text = #"Dashboard";
[self.navigationController.navigationBar addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)];
lbl2.backgroundColor = [UIColor redColor];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[self.navigationController.navigationBar addSubview:lbl2];
}
You can customize the titleView of navigation bar. Read the official documentation here.
As per your question, I tried the below working code:-
-(UIView *)getTitleView
{
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, self.navigationController.navigationBar.frame.size.height)];
lbl1.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
lbl1.textAlignment = NSTextAlignmentCenter;
[lbl1 setTextColor:[UIColor whiteColor]];
lbl1.text = #"Dashboard";
[viewTitle addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, viewTitle.frame.size.height)];
lbl2.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
[lbl2 setTextColor:[UIColor whiteColor]];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[viewTitle addSubview:lbl2];
return viewTitle;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self getTitleView];
}
I divide the code in separate method to follow modular approach. For any part of code, which is of separate task, write that piece of code in different method. This will lead to more readability.

Generated subview is always behind image view

viewController.m
-(void) viewDidLoad
{
[super viewDidLoad];
[self addScoreRowViewWithIndexLeft:(++i) andName:[scoreDict objectForKey:#"name"] andScore:[[scoreDict objectForKey:#"points"] integerValue]];
}
-(void) addScoreRowViewWithIndexLeft:(NSInteger)index andName: (NSString *)name andScore: (NSInteger)score {
UIView *scoreRowView = [[UIView alloc] initWithFrame:CGRectMake(128, 112 + index * 80, 280, 20)];
UILabel *indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 20, 180, 20)];
UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(330, 20, 50, 20)];
UIColor *color = [UIColor darkGrayColor];
[indexLabel setText:[#((int)index) stringValue]];
[indexLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[indexLabel setTextColor:color];
[nameLabel setText:name];
[nameLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[nameLabel setTextColor:color];
[scoreLabel setText:[#((int)score) stringValue]];
[scoreLabel setFont:[UIFont fontWithName:#"DINAlternate-Bold" size:18]];
[scoreLabel setTextColor:color];
[scoreRowView addSubview:indexLabel];
[scoreRowView addSubview:nameLabel];
[scoreRowView addSubview:scoreLabel];
[self.view addSubview:scoreRowView];
}
I added my ImageView in my Storyboard as a subview of my View.
When I delete the ImageView, everything is ok. I get displayed my other code generated subviews. But when I add the ImageView in my Storyboard, it's on top of my other subviews. If I give my ImageView an alpha of 0.3 I can see my subviews shining through.
What's wrong with my code? I also tried;
[self.view bringSubviewToFront:scoreRowView];
but it doesn't work.
What can I do?
Try to change zPosition of your ImageView and scoreRowView. You can do this this way:
Connect your ImageView with your ViewController
In bottom of ViewDidLoad add something like that:
yourImageView.layer.zPosition = 1;
scoreRowView.layer.zPosition = 2;
I'm suspecting that [self.view bringSubviewToFront:scoreRowView]; doesn't work because you invoke it in ViewDidLoad where view in not fully work. You can try to invoke it in e.g. ViewDidAppear and check if that will work.

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.

ScrollViews will not resize

I have struggled with this and maybe I am not understanding how scrollviews work. I am trying to calculate the height automatically for 'self.description' however nothing I do seems to work. I have changed values from the scrollviewframe even tried changing the values using frame.size.height. however it still does not calculate the height. Am i missing something?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
if (self) {
CGRect scrollViewFrame = CGRectMake(0, 0, 460, 590);
scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[self.contentView addSubview:self.scrollView];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = NO;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 67.0, 290, 50)];
self.label.font= [UIFont fontWithName:#"Helvetica-Bold" size:20];
self.label.textColor = [UIColor colorWithRed:33.0f/255.0f green:74.0f/255.0f blue:146.0f/255.0f alpha:1.0f];
self.label.numberOfLines = 0;
self.label.lineBreakMode = NSLineBreakByWordWrapping;
self.label.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:self.label];
//[self.contentView addSubview:self.label];
self.description = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 380.0, 300, 9999)];
self.description.font= [UIFont fontWithName:#"Helvetica-Light" size:15];
self.description.textColor = [UIColor blackColor];
self.description.textAlignment = NSTextAlignmentJustified;
self.description.numberOfLines = 0;
self.description.lineBreakMode = NSLineBreakByWordWrapping;
self.description.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:self.description];
//[self.contentView addSubview:self.description];
self.image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 130, 280, 228)];
self.image.clipsToBounds = YES;
[scrollView addSubview:self.image];
//[self.contentView addSubview:self.image];
float hgt=0; for (UIView *view in scrollView.subviews) hgt+=view.frame.size.height;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width,hgt)];
self.backgroundColor = [UIColor whiteColor];
}
}
Are you trying to resize scrollview or the content size of the scroll?
If you like to resize scrollview, the self.contentView aren't resized and if it is not bigger or equal to scrollview, probably it's your issue.
float contentHeight = 0;
contentHeight = yourLastSubview.frame.origin.x + yourLastSubview.frame.size.height;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width,contentHeight)];
//yourLastSubview is for example your self.description, because self.description' origin.y is the largest (380)
If your scrollView.frame.size.height is bigger than contentHeight, your scrollView won't scroll. For example if your scrollView.frame = CGRectMake(0,0,320,480); and your contentHeight is 400 scrollView won't scroll, but if contentHeight = 500; it will.
If you want to enable bouncing your scrollView when the contentHeight is smaller than your scrollView Height use this: scrollView.alwaysBounceVertical = YES;

UIscrollview frame Changes to super view frame?

I have three label and based on the text contentsize of UIScrollview is increasing.While I removing the Scrollview from super view and creating again this time frame height is increasing bottom control.
Before update:
After creating again:
Please let me know if you unclear to question.
-(void)setDataDynamicallyonScroll
{
if (self.infoScroll) {
[self.infoScroll removeFromSuperview];
self.infoScroll = nil;
}
self.infoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 55.0f, 300, 450)];
self.infoScroll.autoresizingMask = UIViewAutoresizingFlexibleHeight;
UILabel *mainTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
UILabel *detailsTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
if ([[self.dataDic valueForKey:#"subData"] valueForKey:#"FirstText"]!=nil) {
NSString *labelText = [[self.dataDic valueForKey:#"subData"] valueForKey:#"FirstText"];
[mainTitleLabel setText:labelText];
// Tell the label to use an unlimited number of lines
[mainTitleLabel setNumberOfLines:0];
[mainTitleLabel setBackgroundColor:[UIColor clearColor]];
[mainTitleLabel setFont:[UIFont boldSystemFontOfSize:12]];
mainTitleLabel.textColor = [UIColor colorWithRed:13.0/255.0 green:52.0/255.0 blue:112.0/255.0 alpha:1.0];
//[infoLabel sizeToFit];
CGSize infoLabelSize = [mainTitleLabel.text sizeWithFont:mainTitleLabel.font
constrainedToSize:CGSizeMake(self.infoScroll.frame.size.width, 9000)
lineBreakMode:NSLineBreakByWordWrapping];
mainTitleLabel.frame = CGRectMake(0, 0, infoLabelSize.width, infoLabelSize.height);
self.infoScroll.contentSize = infoLabelSize;
NSLog(#"%f,%f,%f,%f ",mainTitleLabel.frame.origin.x,mainTitleLabel.frame.origin.y,mainTitleLabel.frame.size.width,mainTitleLabel.frame.size.height);
NSLog(#"for First %f",self.infoScroll.contentSize.height);
[self.infoScroll addSubview:mainTitleLabel];
}
if ([[self.dataDic valueForKey:#"subData"] valueForKey:#"MiddleText"]!=nil) {
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
switch ([delegate langType]) {
case 0:
[subTitleLabel setText:[[[self.dataDic valueForKey:#"subData"] valueForKey:#"MiddleText"] valueForKey:#"English"]];
break;
case 1:
[subTitleLabel setText:[#"\u200F" stringByAppendingString:[[[self.dataDic valueForKey:#"subData"] valueForKey:#"MiddleText"] valueForKey:#"Hebrew"]]];
break;
case 2:
[subTitleLabel setText:[[[self.dataDic valueForKey:#"subData"] valueForKey:#"MiddleText"] valueForKey:#"Trans"]];
break;
default:
break;
}
// Tell the label to use an unlimited number of lines
[subTitleLabel setNumberOfLines:0];
[subTitleLabel setBackgroundColor:[UIColor clearColor]];
[subTitleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[subTitleLabel setTextAlignment:NSTextAlignmentRight];
subTitleLabel.textColor = [UIColor colorWithRed:155.0/255.0 green:0/255.0 blue:16.0/255.0 alpha:1.0];
//[infoLabel sizeToFit];
CGSize infoLabelSize = [subTitleLabel.text sizeWithFont:subTitleLabel.font
constrainedToSize:CGSizeMake(self.infoScroll.frame.size.width, 9000)
lineBreakMode:NSLineBreakByWordWrapping];
subTitleLabel.frame = CGRectMake(0, self.infoScroll.contentSize.height+20, infoLabelSize.width, infoLabelSize.height);
CGSize updatedScrollViewContentSize = CGSizeMake(300,self.infoScroll.contentSize.height+infoLabelSize.height+20 );
self.infoScroll.contentSize = updatedScrollViewContentSize;
[self.infoScroll addSubview:subTitleLabel];
}
if ([[self.dataDic valueForKey:#"subData"] valueForKey:#"LastText"]!=nil) {
[detailsTitleLabel setText:[[self.dataDic valueForKey:#"subData"] valueForKey:#"LastText"]];
// Tell the label to use an unlimited number of lines
[detailsTitleLabel setNumberOfLines:0];
[detailsTitleLabel setBackgroundColor:[UIColor clearColor]];
[detailsTitleLabel setFont:[UIFont systemFontOfSize:13]];
[detailsTitleLabel setTextAlignment:NSTextAlignmentLeft];
detailsTitleLabel.textColor = [UIColor blackColor];
[detailsTitleLabel sizeToFit];
CGSize infoLabelSize = [detailsTitleLabel.text sizeWithFont:detailsTitleLabel.font
constrainedToSize:CGSizeMake(self.infoScroll.frame.size.width, 9000)
lineBreakMode:NSLineBreakByWordWrapping];
UIImageView *bgView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"detailsTextbg.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)]];
bgView.frame = CGRectMake(0,self.infoScroll.contentSize.height+20, infoLabelSize.width, infoLabelSize.height+20);
detailsTitleLabel.frame = CGRectMake(5,10, infoLabelSize.width, infoLabelSize.height);
CGSize updatedScrollViewContentSize = CGSizeMake(300,self.infoScroll.contentSize.height+detailsTitleLabel.frame.size.height+50);
self.infoScroll.contentSize = updatedScrollViewContentSize;
[bgView addSubview:detailsTitleLabel];
[self.infoScroll addSubview:bgView];
}
[self.view addSubview:self.infoScroll];
}
It could be because of this line: self.infoScroll.autoresizingMask = UIViewAutoresizingFlexibleHeight; If you are manually setting the frame, I do not believe you want it to autoResize.

Resources