how to set dynamic height for all iphone screen - ios

In my application dn't use autolayout and size classes. Create all the elements programmatically.
Calculate view width and set frame for each element. Here my code.
UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,(self.view.frame.size.height/2)-235,100, 50)];
TitleLbl.text=#"Login";
TitleLbl.textColor=[UIColor whiteColor];
TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
[self.view addSubview:TitleLbl];
//NSLog(#"ttttt %#",(self.view.frame.size.height/10));
UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,(self.view.frame.size.height/2)-210,350, 50)];
AccountLbl.text=#"Login or Create Your New Account";
AccountLbl.textColor=[UIColor whiteColor];
AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
[self.view addSubview:AccountLbl];
UITextField* UserNameFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, (self.view.frame.size.height/2)-175,self.view.frame.size.width-80, 40)];
UserNameFld.font = [UIFont systemFontOfSize:15];
UserNameFld.placeholder = #"Username";
UserNameFld.keyboardType = UIKeyboardTypeDefault;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Username" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
UserNameFld.attributedPlaceholder = str;
UserNameFld.delegate = self;
[self DrawLine: UserNameFld];
[self.view addSubview:UserNameFld];
UITextField* PassWorldFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, (self.view.frame.size.height/2)-135,self.view.frame.size.width-80, 40)];
PassWorldFld.font = [UIFont systemFontOfSize:15];
PassWorldFld.placeholder = #"Password";
PassWorldFld.keyboardType = UIKeyboardTypeDefault;
PassWorldFld.secureTextEntry = YES;
PassWorldFld.delegate = self;
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:#"Password" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
PassWorldFld.attributedPlaceholder = strPassword;
[self DrawLine: PassWorldFld];
[self.view addSubview:PassWorldFld];
UIButton *ForPassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [ForPassBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[ForPassBtn setTitle:#"Forgot Password?" forState:UIControlStateNormal];
ForPassBtn.frame = CGRectMake((self.view.frame.size.width/2), (self.view.frame.size.height/2)-90, (self.view.frame.size.width/2)-40, 20.0);
ForPassBtn.layer.borderColor = [UIColor whiteColor].CGColor;
ForPassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
ForPassBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
ForPassBtn.backgroundColor=[UIColor clearColor];
[self.view addSubview:ForPassBtn];
UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [LoginBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[LoginBtn setTitle:#"Login" forState:UIControlStateNormal];
LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,(self.view.frame.size.height/2)-65, self.view.frame.size.width-80, 30.0);
LoginBtn.layer.cornerRadius = 10;
LoginBtn.layer.borderWidth=1.0f;
LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
[self.view addSubview:LoginBtn];
How can i calulate the dynamic heigth for this code.
Any one can help me. how to calulate dynamic heigth. give any solution for me. Thanks advance.

Write below in .pch file
#define iPhoneFactorX ([[UIScreen mainScreen] bounds].size.width*1.00/1080.0)
Now write a code as per screen size of 1080x1920
E.g. you want to set a UIImage of size 600*400
UIImageView *m1 = [[UIImageView alloc] initWithFrame:CGRectMake(240*iPhoneFactorX, 100*iPhoneFactorX, 600*iPhoneFactorX, 400*iPhoneFactorX)];
Below is how I calculate starting point as 240
(1080-600)/2
^^^ width of image
This make 1 code for all screens....

UILabel* TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-25,(self.view.frame.size.height/2)-235,100, 50)];
TitleLbl.text=#"Login";
TitleLbl.textColor=[UIColor whiteColor];
TitleLbl.font = [UIFont boldSystemFontOfSize:14.0];
[self.view addSubview:TitleLbl];
//NSLog(#"ttttt %#",(self.view.frame.size.height/10));
UILabel* AccountLbl = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width/2)-130,TitleLbl.frame.origin.y + TitleLbl.frame.size.height + 10,350, 50)];
AccountLbl.text=#"Login or Create Your New Account";
AccountLbl.textColor=[UIColor whiteColor];
AccountLbl.font = [UIFont boldSystemFontOfSize:16.0];
[self.view addSubview:AccountLbl];
UITextField* UserNameFld = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.origin.x+40, AccountLbl.frame.origin.y + AccountLbl.frame.size.height + 10,self.view.frame.size.width-80, 40)];
UserNameFld.font = [UIFont systemFontOfSize:15];
UserNameFld.placeholder = #"Username";
UserNameFld.keyboardType = UIKeyboardTypeDefault;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:#"Username" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
UserNameFld.attributedPlaceholder = str;
UserNameFld.delegate = self;
// [self DrawLine: UserNameFld];
[self.view addSubview:UserNameFld];
UITextField* PassWorldFld = [[UITextField alloc] initWithFrame:CGRectMake(UserNameFld.frame.origin.x, UserNameFld.frame.origin.y + UserNameFld.frame.size.height + 10,UserNameFld.frame.origin.x + UserNameFld.frame.size.width, 40)];
PassWorldFld.font = [UIFont systemFontOfSize:15];
PassWorldFld.placeholder = #"Password";
PassWorldFld.keyboardType = UIKeyboardTypeDefault;
PassWorldFld.secureTextEntry = YES;
PassWorldFld.delegate = self;
NSAttributedString *strPassword = [[NSAttributedString alloc] initWithString:#"Password" attributes:#{ NSForegroundColorAttributeName : [UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0] }];
PassWorldFld.attributedPlaceholder = strPassword;
// [self DrawLine: PassWorldFld];
[self.view addSubview:PassWorldFld];
UIButton *ForPassBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [ForPassBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[ForPassBtn setTitle:#"Forgot Password?" forState:UIControlStateNormal];
ForPassBtn.frame = CGRectMake((self.view.frame.size.width/2), PassWorldFld.frame.origin.y + PassWorldFld.frame.size.height + 50, (self.view.frame.size.width/2)-40, 20.0);
ForPassBtn.layer.borderColor = [UIColor whiteColor].CGColor;
ForPassBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
ForPassBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
ForPassBtn.backgroundColor=[UIColor clearColor];
[self.view addSubview:ForPassBtn];
UIButton *LoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// [LoginBtn addTarget:self action:#selector(aMethod:)
// forControlEvents:UIControlEventTouchUpInside];
[LoginBtn setTitle:#"Login" forState:UIControlStateNormal];
LoginBtn.frame = CGRectMake(self.view.frame.origin.x+40,ForPassBtn.frame.origin.y + ForPassBtn.frame.size.height + 50, self.view.frame.size.width-80, 30.0);
LoginBtn.layer.cornerRadius = 10;
LoginBtn.layer.borderWidth=1.0f;
LoginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
LoginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
LoginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
[self.view addSubview:LoginBtn];

Related

How to align custom UINavigationBar title (UILabel )in middle (iOS)

I am using following code to align 2 strings in UINavigationBar centre .
for ex:
<Back John Kelly Maria
23/F
I need these kind of middle aligned text in UINavigationbar but what jam now getting is all the text aligned in right side of UINavigationBar .
something like this
<Back John Kelly Maria
23/F
Please help..me .
this is my code
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)];
UIFont * customFont = [UIFont fontWithName:#"Helvetica Neue" size:19]; //custom font
NSString * text =title;
UILabel *patientNameLabel;
if([title length]>15){
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}
patientNameLabel.text = #"John Kelly Maria";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =subTitleText;
UIFont * customFont1 = [UIFont fontWithName:#"Helvetica Neue" size:14]; //custom font
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.text = #"23/F";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];
[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
Add below two lines before addSubview lines..
patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 0);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 25);
Add Below Code & see
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];
UIFont * customFont = [UIFont fontWithName:#"Helvetica Neue" size:19]; //custom font
NSString * text =#"Nilesh Patel";
UILabel *patientNameLabel;
if([text length]>15){
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}
patientNameLabel.text = #"Nilesh Patel";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =#"iOS Developer";
UIFont * customFont1 = [UIFont fontWithName:#"Helvetica Neue" size:14]; //custom font
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.text = #"28/M";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];
patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 10);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 30);
[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
[self.navigationController.navigationBar addSubview:titleView];
Note : You have set static frame to titleView so if you see your app on different device you will not be able to see the text in exact centre. To avoid this set the titleView frame dynamically based on device/view width.
Try Frame setting like this
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,20,screenWidth,50)];
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,screenWidth,20)];
UILabel *patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,screenWidth,30)];

iOS- Objective C - Show a view on ipad only but hide on iPhone

I need a solution for the problem described below..
I have a scenario where a view needs to be shown only on ipad but it needs to be hidden on iphone. The way the view is built is as follows:
The below code shows the view on ipad. I want to hide this view when loaded on iphone.
How can this be achieved?
(void)viewDidLoad
{
[super viewDidLoad];
gotQuoteList = NO;
isExpanded = NO;
heightOfUploadManager = 282;
freqQuotesArray = [[NSMutableArray alloc] init];
selectedCheckboxes = [[NSMutableDictionary alloc] init];
serviceConfig_G_Obj = [[ServiceConfig alloc] init];
cacheManager_G_Obj = [[CacheManager alloc] init];
[self.view setBackgroundColor: [[UIColor whiteColor] colorWithAlphaComponent:0.5f]];
[self observeRotationChanges];
uploadMngrView = [[UIView alloc] init];
[self setUploadManagerFrame];
[uploadMngrView setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
uploadMngrView.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
uploadMngrView.layer.borderWidth = 1.0;
[self.view addSubview:uploadMngrView];
UIView *titleLblBgView = [[UIView alloc] init];
[titleLblBgView setFrame:CGRectMake(0, 40, 512, 1)];
[titleLblBgView setBackgroundColor:[self colorWithHexString:#"C7C7C7"]];
[uploadMngrView addSubview:titleLblBgView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 1, 512-21, 38)];
[titleLabel setText:#"Upload Document"];
[titleLabel setTextAlignment:NSTextAlignmentLeft];
[titleLabel setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
[titleLabel setTextColor:[self colorWithHexString:#"656565"]];
[titleLabel setFont:[UIFont fontWithName:#"Arial" size:21]];
[uploadMngrView addSubview:titleLabel];
UILabel *fileLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 141, 34)];
[fileLabel setText:#"File"];
[fileLabel setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
[fileLabel setTextColor:[self colorWithHexString:#"656565"]];
[fileLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[uploadMngrView addSubview:fileLabel];
UILabel *fileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(163, 60, 329, 34)];
[fileNameLabel setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:#"labelBG.png"]]];
[fileNameLabel setTextColor:[self colorWithHexString:#"656565"]];
[fileNameLabel setText:[NSString stringWithFormat:#" %#",[[self getFileName] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[fileNameLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
fileNameLabel.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
fileNameLabel.layer.borderWidth = 1.0;
[uploadMngrView addSubview:fileNameLabel];
UILabel *destinationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 114, 141, 34)];
[destinationLabel setTextColor:[self colorWithHexString:#"656565"]];
[destinationLabel setText:#"Destination"];
[destinationLabel setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
[destinationLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[uploadMngrView addSubview:destinationLabel];
UILabel *destinationNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(163, 114, 329, 34)];
destinationNameLabel.tag = 1;
UITapGestureRecognizer *worksaceRBTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapRadioBtnLabelWithGesture:)];
worksaceRBTapGestureRecognizer.numberOfTapsRequired = 1;
[destinationNameLabel setUserInteractionEnabled:YES];
[destinationNameLabel addGestureRecognizer:worksaceRBTapGestureRecognizer];
destinationNameLabel.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
destinationNameLabel.layer.borderWidth = 1.0;
[destinationNameLabel setText:#" My Workspace"];
[destinationNameLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"labelBG.png"]]]; [destinationNameLabel setTextColor:[self colorWithHexString:#"656565"]];
[destinationNameLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[uploadMngrView addSubview:destinationNameLabel];
myWorkspaceRadioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
myWorkspaceRadioBtn.tag = 1;
[myWorkspaceRadioBtn setImage:[UIImage imageNamed:#"radiobtn_off.png"] forState:UIControlStateNormal];
[myWorkspaceRadioBtn setImage:[UIImage imageNamed:#"radiobtn_on.png"] forState:UIControlStateSelected];
[myWorkspaceRadioBtn setFrame:CGRectMake(166, 121, 26, 22)];
myWorkspaceRadioBtn.selected=YES;
[myWorkspaceRadioBtn addTarget:self action:#selector(radioButton:) forControlEvents:UIControlEventTouchUpInside];
[uploadMngrView addSubview:myWorkspaceRadioBtn];
UILabel *destinationQuoteLabel = [[UILabel alloc] initWithFrame:CGRectMake(163, 147, 329, 34)];
destinationQuoteLabel.tag = 2;
UITapGestureRecognizer *quotelistRBTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapRadioBtnLabelWithGesture:)];
quotelistRBTapGestureRecognizer.numberOfTapsRequired = 1;
[destinationQuoteLabel setUserInteractionEnabled:YES];
[destinationQuoteLabel addGestureRecognizer:quotelistRBTapGestureRecognizer];
destinationQuoteLabel.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
destinationQuoteLabel.layer.borderWidth = 1.0;
[destinationQuoteLabel setTextColor:[self colorWithHexString:#"656565"]];
[destinationQuoteLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
[destinationQuoteLabel setText:#" Link to selected quote"];
[destinationQuoteLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"labelBG.png"]]];
[uploadMngrView addSubview:destinationQuoteLabel];
quoteListRadioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
quoteListRadioBtn.tag = 2;
[quoteListRadioBtn setImage:[UIImage imageNamed:#"radiobtn_off.png"] forState:UIControlStateNormal];
[quoteListRadioBtn setImage:[UIImage imageNamed:#"radiobtn_on.png"] forState:UIControlStateSelected];
[quoteListRadioBtn setFrame:CGRectMake(166, 152, 26, 22)];
[quoteListRadioBtn addTarget:self action:#selector(radioButton:) forControlEvents:UIControlEventTouchUpInside];
[uploadMngrView addSubview:quoteListRadioBtn];
[self fillQuoteListScrollView];
lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 202, 512, 1)];
lineView.backgroundColor = [self colorWithHexString:#"C7C7C7"];
[uploadMngrView addSubview:lineView];
cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setFrame:CGRectMake(20, 222, 231, 40)];
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[cancelButton setTitleColor:[self colorWithHexString:#"656565"] forState:UIControlStateNormal];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
CAGradientLayer *cancelBtnGradient = [CAGradientLayer layer];
cancelBtnGradient.frame = cancelButton.bounds;
cancelBtnGradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:255.0/255.0f green:255.0/255.0f blue:255.0/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:238.0/255.0f green:238.0/255.0f blue:238.0/255.0f alpha:1.0f] CGColor], nil];
[cancelButton.layer insertSublayer:cancelBtnGradient atIndex:0];
cancelButton.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
cancelButton.layer.borderWidth = 1.0;
[cancelButton addTarget:self action:#selector(cancelUpload) forControlEvents:UIControlEventTouchUpInside];
[uploadMngrView addSubview:cancelButton];
uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[uploadButton setFrame:CGRectMake(261, 222, 231, 40)];
[uploadButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
[uploadButton setTitleColor:[self colorWithHexString:#"55A0B9"] forState:UIControlStateNormal];
[uploadButton setTitle:#"Upload" forState:UIControlStateNormal];
CAGradientLayer *uploadBtnGradient = [CAGradientLayer layer];
uploadBtnGradient.frame = uploadButton.bounds;
uploadBtnGradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:255.0/255.0f green:255.0/255.0f blue:255.0/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:238.0/255.0f green:238.0/255.0f blue:238.0/255.0f alpha:1.0f] CGColor], nil];
[uploadButton.layer insertSublayer:uploadBtnGradient atIndex:0];
uploadButton.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
uploadButton.layer.borderWidth = 1.0;
[uploadButton addTarget:self action:#selector(uploadDoc) forControlEvents:UIControlEventTouchUpInside];
[uploadMngrView addSubview:uploadButton];
}
You can do this like this,
uploadMngrView = [[UIView alloc] init];
[self setUploadManagerFrame];
[uploadMngrView setBackgroundColor:[self colorWithHexString:#"EFEFEF"]];
uploadMngrView.layer.borderColor = [self colorWithHexString:#"C7C7C7"].CGColor;
uploadMngrView.layer.borderWidth = 1.0;
if ([UIDevice currentDevice].userInterfaceIdom == UIUserInterfaceIdoniPad) {
[self.view addSubview:uploadMngrView];
}
HTH, Enjoy Coding !!
As your setting up the views programatically, you something like this
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Create the view only for ipad
self.someView = [[UIView alloc] initWithFrame:someFrame];
//Or set the view hidden just on ipad
self.someView.hidden = YES;
//Or set the view a different frame on ipad
self.someView.frame = CGRectMake(x,y,width,height);
}
else
{
//Do some non ipad layout stuff here
}

Warning A long-running Parse operation is being executed on the main thread? [duplicate]

This question already has answers here:
A long-running Parse operation is being executed on the main thread
(4 answers)
Closed 7 years ago.
A long-running Parse operation is being executed on the main thread is happening on this part of code from below, i can't seem to figure out how to rid the error. Any help on how to get rid of warning.
PFFile *image = (PFFile *)[wallObject objectForKey:KEY_IMAGE];
UIImageView *userImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]];
userImage.frame = CGRectMake(0, 65, wallImageView.frame.size.width, 225);
[wallImageView addSubview:userImage];
here is code below
-(void)loadWallViews
{
//Clean the scroll view
for (id viewToRemove in [self.wallScroll subviews]){
if ([viewToRemove isMemberOfClass:[UIView class]])
[viewToRemove removeFromSuperview];
}
//For every wall element, put a view in the scroll
int originY = 10;
for (PFObject *wallObject in self.imageFilesArray){
//Build the view with the image and the comments
UIView *wallImageView = [[UIView alloc] initWithFrame:CGRectMake(10, originY, self.view.frame.size.width - 20 , 330)]; //self.view.frame.size.height
//[[UIView appearance] setBackgroundColor:[UIColor redColor]]; //added for problem solve
//Add the image
PFFile *image = (PFFile *)[wallObject objectForKey:KEY_IMAGE];
UIImageView *userImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]];
userImage.frame = CGRectMake(0, 65, wallImageView.frame.size.width, 225);
[wallImageView addSubview:userImage];
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, wallImageView.frame.size.width,55)];
infoLabel.text = [wallObject objectForKey:#"newsTitle"];
infoLabel.font = [UIFont fontWithName:KEY_FONT size:20];
infoLabel.textColor = [UIColor darkGrayColor];
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.numberOfLines = 0;
[wallImageView addSubview:infoLabel];
//Add the info label (User and creation date)
NSDate *creationDate = wallObject.createdAt;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:KEY_DATEFORMAT];
//Add the comment
UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, wallImageView.frame.size.width, 12)];
//commentLabel.text = [NSString stringWithFormat:#"Uploaded by: %#, %#", [wallObject objectForKey:#"NewsDetail"], [df stringFromDate:creationDate]];
commentLabel.text = [NSString stringWithFormat:#": %#, %#", [wallObject objectForKey:#"newsDetail"], [df stringFromDate:creationDate]];
commentLabel.font = [UIFont fontWithName:KEY_FONT size:10];
commentLabel.textColor = [UIColor lightGrayColor];
commentLabel.backgroundColor = [UIColor clearColor];
[wallImageView addSubview:commentLabel];
UILabel *readLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 71 , 50, wallImageView.frame.size.width, 12)];
readLabel.text = #"Read more";
readLabel.font = [UIFont fontWithName:KEY_FONT size:10];
readLabel.textColor = [UIColor blueColor];
readLabel.backgroundColor = [UIColor clearColor];
[wallImageView addSubview:readLabel];
UIButton *faceBtn = [[UIButton alloc] initWithFrame:CGRectMake(2,310, 20, 20)];
[faceBtn setImage:[UIImage imageNamed:#"Facebook.png"] forState:UIControlStateNormal];
[faceBtn addTarget:self action:#selector(share:) forControlEvents:UIControlEventTouchUpInside];
[wallImageView addSubview:faceBtn];
UIButton *twitBtn = [[UIButton alloc] initWithFrame:CGRectMake(32,310, 20, 20)];
[twitBtn setImage:[UIImage imageNamed:#"Twitter.png"] forState:UIControlStateNormal];
[twitBtn addTarget:self action:#selector(share:) forControlEvents:UIControlEventTouchUpInside];
[wallImageView addSubview:twitBtn];
UIButton *tumblrBtn = [[UIButton alloc] initWithFrame:CGRectMake(62,310, 20, 20)];
[tumblrBtn setImage:[UIImage imageNamed:#"Tumblr.png"] forState:UIControlStateNormal];
[tumblrBtn addTarget:self action:#selector(share:) forControlEvents:UIControlEventTouchUpInside];
[wallImageView addSubview:tumblrBtn];
UIButton *yourBtn = [[UIButton alloc] initWithFrame:CGRectMake(92,310, 20, 20)];
[yourBtn setImage:[UIImage imageNamed:#"Flickr.png"] forState:UIControlStateNormal];
[yourBtn addTarget:self action:#selector(share:) forControlEvents:UIControlEventTouchUpInside];
[wallImageView addSubview:yourBtn];
// wallImageView = UIEdgeInsetsMake(0.0f, myCell.frame.size.width, 0.0f, 400.0f);
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 343, self.view.frame.size.width, .8)];
separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
[wallImageView addSubview:separatorLineView];
[self.wallScroll addSubview:wallImageView];
originY = originY + wallImageView.frame.size.width + 1;
}
//Set the bounds of the scroll
self.wallScroll.contentSize = CGSizeMake(self.wallScroll.frame.size.width, originY);
//Remove the activity indicator
[self.activityIndicator stopAnimating];
[self.activityIndicator removeFromSuperview];
It's probably this:
[[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]]
Looks like you're getting images synchronously instead of asynchronously.
Instead of using UIImageView, you should use PFImageView, which will download the file asynchronously if it's not already cached.

IOS, setting up Gesture Tap on UILabel thats inside a UIScrollView

I have a UIScrollView and inside this Scroll View i have some controls. Specifically a label thats underlined and i have setup Tap Gesture for the UILabel but doesn't seem to fire the methods that the Tap Gesture is bound to.
Here is the code:
-(void)setupUserNameControls:(UIScrollView*)scroll{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 23, 70, 16)];
label.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
label.text = #"First Name";
label.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:label];
UIView *textView = [[UIView alloc] initWithFrame:CGRectMake(88, 16, 215, 28)];
textView.backgroundColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
[scroll addSubview:textView];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(15, 55, 70, 16)];
label2.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
label2.text = #"Last Name";
label2.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:label2];
UIView *textView2 = [[UIView alloc] initWithFrame:CGRectMake(88, 48, 215, 28)];
textView2.backgroundColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
[scroll addSubview:textView2];
UIImageView *imageViewSep = [[UIImageView alloc] initWithFrame:CGRectMake(10, 92, 300, 2)];
UIImage *imageSep = [UIImage imageNamed:#"seperator.png"];
imageViewSep.image = imageSep;
[scroll addSubview:imageViewSep];
UIView *viewImage = [[UIView alloc] initWithFrame:CGRectMake(10, 108, 58, 58)];
viewImage.backgroundColor = [UIColor colorWithRed:132.0f/255 green:132.0f/255 blue:132.0f/255 alpha:1.0f];
[scroll addSubview:viewImage];
UIImageView *imageViewUser = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 48, 48)];
UIImage *imageUser = [UIImage imageNamed:#"defaultuser.jpg"];
imageViewUser.image = imageUser;
[viewImage addSubview:imageViewUser];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Click to select your user image"];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
UILabel *labelPickImage = [[UILabel alloc] initWithFrame:CGRectMake(88, 126, 215, 16)];
labelPickImage.numberOfLines = 0;
labelPickImage.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
labelPickImage.attributedText = attributeString;
labelPickImage.font = [UIFont fontWithName:#"Helvetica" size:14];
[scroll addSubview:labelPickImage];
UITapGestureRecognizer *userImageTextTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(userImageSelectTap:)];
[userImageTextTap setNumberOfTouchesRequired:1];
[userImageTextTap setNumberOfTapsRequired:1];
[labelPickImage addGestureRecognizer:userImageTextTap];
}
-(void)userImageSelectTap:(UIGestureRecognizer *)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}else{
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker {
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
This is how i set up the UIScrollView and pass it in:
UIScrollView *userInfoScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 108, screenRect.size.width, screenRect.size.height - 40 - viewTop.bounds.size.height - viewTopBorder.bounds.size.height)];
[userInfoScrollView setBackgroundColor:[UIColor colorWithRed:228.0f/255 green:228.0f/255 blue:228.0f/255 alpha:1.0f]];
userInfoScrollView.userInteractionEnabled = YES;
[self.viewUser addSubview:userInfoScrollView];
make sure the label stays in front of the scrollView.
[scroll bringSubviewToFront:labelPickImage];
Also,
labelPickImage.userInteractionEnabled = YES;

How to add multiple UILabels programmatically in Xcode?

Hello in Xcode I am trying to add two labels to my image programmatically but when I add the second one it will overwrite my first one. Here is my code:
//first label
UILabel* caption = [[UILabel alloc] initWithFrame:
CGRectMake(-25, 0, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentLeft;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" #%#",[data objectForKey:#"username"]];
[self addSubview: caption];
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:
CGRectMake(-25, -200, 320, 50)
];
caption.backgroundColor = [UIColor whiteColor];
caption.textColor = [UIColor blackColor];
caption.textAlignment = UITextAlignmentCenter;
caption.font = [UIFont systemFontOfSize:16];
caption.text = [NSString stringWithFormat:#" Testing"];
[self addSubview: bottomBox];
I've tried changing the "0 to -200 to modify the Y coordinates so that the second label will shift down but it just overwrites the first one for some reason. Any help would be greatly appreciated thanks.
All the code for your 2nd label is referencing the 1st variable caption instead of bottomBox. You want:
//second label
UILabel* bottomBox = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 320, 50)];
bottomBox.backgroundColor = [UIColor whiteColor];
bottomBox.textColor = [UIColor blackColor];
bottomBox.textAlignment = UITextAlignmentCenter;
bottomBox.font = [UIFont systemFontOfSize:16];
bottomBox.text = #"Testing";
[self addSubview: bottomBox];

Resources