objective-c ios auto layout NSLayoutConstraint - objective-c

UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, frameWidth, frameHeight)];
headerView.backgroundColor = UIColorFromRGB(0x5F70B9);
UIImage* leftImage = [UIImage imageNamed: #"search_list"];
UIImageView* leftImageView = [[UIImageView alloc] initWithImage: leftImage];
[leftImageView setFrame: CGRectMake(10, 15, 70, 50)]; // CGRectMake(10, 15, 70, 50)
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(80, 20, 160, 40)];
textView.backgroundColor = UIColorFromRGB(0x5F70B9);
textView.textColor = UIColorFromRGB(0xFFFFFF);
[textView setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3]];
textView.text = STLocalizedString(#"message_overall_non_reply");
textView.editable = NO;
UIImage* rightImage = [UIImage imageNamed: #"arrow_mask"];
UIImageView* rightImageView = [[UIImageView alloc] initWithImage: rightImage];
[rightImageView setFrame:CGRectMake(380, 30, 15, 20)];
[headerView addSubview: leftImageView];
[headerView addSubview: textView];
[headerView addSubview: rightImageView];
Expect image:
enter image description here
Now i am hardcoding the origin x, y and everything. I want to use auto layout so that the left image and right image act as leading and trailing icon.
any suggestion?
Tried something like below, but not working:
`
[leftImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *leftImageViewConstraint = [NSLayoutConstraint constraintWithItem: leftImage attribute: NSLayoutAttributeLeading relatedBy: NSLayoutRelationEqual toItem: headerView attribute: NSLayoutAttributeLeading multiplier: 1 constant: 0];
[leftImageView addConstraints: #[leftImageViewConstraint]];
`

You need to activate each constraint that you add. When you have a bunch of constraints to set it's a lot easier to use NSLayoutConstraint activateConstraints. And using the various "anchor" properties for setting up the constraints is simpler than using the long form of creating an NSLayoutConstraint.
Here's your code updated with lots of constraints:
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(8, 8, frameWidth, frameHeight)];
headerView.backgroundColor = UIColorFromRGB(0x5F70B9);
UIImage* leftImage = [UIImage imageNamed: #"search_list"];
UIImageView* leftImageView = [[UIImageView alloc] initWithImage: leftImage];
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.backgroundColor = UIColorFromRGB(0x5F70B9);
textView.textColor = UIColorFromRGB(0xFFFFFF);
textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle3];
textView.text = STLocalizedString(#"message_overall_non_reply");
textView.editable = NO;
UIImage* rightImage = [UIImage imageNamed: #"arrow_mask"];
UIImageView* rightImageView = [[UIImageView alloc] initWithImage: rightImage];
leftImageView.translatesAutoresizingMaskIntoConstraints = NO;
textView.translatesAutoresizingMaskIntoConstraints = NO;
rightImageView.translatesAutoresizingMaskIntoConstraints = NO;
[headerView addSubview: leftImageView];
[headerView addSubview: textView];
[headerView addSubview: rightImageView];
[NSLayoutConstraint activateConstraints:#[
[leftImageView.leadingAnchor constraintEqualToAnchor:headerView.leadingAnchor constant:10],
//[leftImageView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:15],
[leftImageView.centerYAnchor constraintEqualToAnchor:headerView.centerYAnchor],
[leftImageView.widthAnchor constraintEqualToConstant:70],
[leftImageView.heightAnchor constraintEqualToConstant:50],
[rightImageView.trailingAnchor constraintEqualToAnchor:headerView.trailingAnchor constant:-10],
//[rightImageView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:30],
[rightImageView.centerYAnchor constraintEqualToAnchor:headerView.centerYAnchor],
[rightImageView.widthAnchor constraintEqualToConstant:15],
[rightImageView.heightAnchor constraintEqualToConstant:20],
[textView.leadingAnchor constraintEqualToAnchor:leftImageView.trailingAnchor constant:10],
[textView.trailingAnchor constraintEqualToAnchor:rightImageView.leadingAnchor constant:-10],
[textView.topAnchor constraintEqualToAnchor:headerView.topAnchor constant:20],
[textView.bottomAnchor constraintEqualToAnchor:headerView.bottomAnchor constant:-20],
]];
I made a few guesses here. Obviously you can change these to suit your needs.

Related

adding UIlabel to camera overlay view uiimagepickercontroller Xcode objc

I want to add some UILabels on top of my UIImagePickerController's custom OverlayView but can't seem to get it working.
Here is my code.
-(IBAction)getPhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera ;
picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.toolbarHidden = YES;
OverlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,1024)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(OverlayView.frame.size.height/2, OverlayView.frame.size.width/2, 30, 100)];
label.textColor = [UIColor whiteColor];
label.text = #"find Me";
[self.OverlayView addSubview:label];
[self.OverlayView bringSubviewToFront:label];
picker.cameraOverlayView = OverlayView;
CGSize screenSize = [[UIScreen mainScreen] bounds].size; // 320 x 568
float scale = screenSize.height / screenSize.width*3/4; // screen height divided by the pickerController height ... or: 568 / ( 320*4/3 )
CGAffineTransform translate=CGAffineTransformMakeTranslation(0,(screenSize.height - screenSize.width*4/3)*0.5);
CGAffineTransform fullScreen=CGAffineTransformMakeScale(scale, scale);
picker.cameraViewTransform =CGAffineTransformConcat(fullScreen, translate);
[self presentViewController: picker animated:YES completion:NULL];
}
the overview shows up and it is full screen but no label
any ideas
Your label is just offscreen, check its frame
You can place it for instance like that, at least it will be visible
self.overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.overlayView.frame.size.width/2, self.overlayView.frame.size.height/2, 30, 100)];

White line on the bottom on ios 7 with UITextField

I have text field, i need to show the cursor and hide the keyboard. It looks ok in the IOS 6 and IOS 5. But in IOS 7 i can see small white line on the bottom.
My code is
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[scrollview setDelegate:self];
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
[scrollview setBackgroundColor:[UIColor blackColor]];
scrollview.contentSize = CGSizeMake(768, 1024);
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 500, 100)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.returnKeyType = UIReturnKeyDone;
[textField setUserInteractionEnabled:YES];
[textField setEnabled:YES];
[scrollview addSubview:textField];
// Here I need to show the cursor without showing keyboard
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
((UITextField*)textField).inputView = dummyView;
[((UITextField*)textField) becomeFirstResponder];
Anyone having any thought how to fix this?
I think this is due to the keyboard view, whose having the height as 1.
So if you change the line
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
to
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
it will work.

Strange behaviour when laying out subviews

I am creating a UI that the user can page through multiple views by using a UIScrollView. I create the UIView objects in code and add them to UIScrollView. I use the code below to create the views.
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *brandView = [[UIView alloc] initWithFrame:frame];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 50.0, frame.size.width, 30)];
label.text = [brand objectForKey:#"name"];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
[brandView addSubview:label];
UIImageView *logoImageView = [[UIImageView alloc] init];
logoImageView.frame = CGRectMake(20.0, 100.0, 280.0, 300.0);
[brandView addSubview:logoImageView];
logoImageView.file = [brand objectForKey:#"logo"];
[logoImageView loadInBackground];
Although I give 50.0 for the y coordinate for the label, I can't see the label at all when I run the app. And logoImageView has 100.0 for its top but it appears just below the status bar, as if it had 0.0 for it's y coordinate.
What is the reason for this strange behaviour, am I doing something wrong?
Note: Auto layout for the UIScrollView is disabled on IB.
Below code will work for you !
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * 10;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *brandView = [[UIView alloc] init]; // CHANGE THAT HAS TO BE DONE
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 50.0, self.scrollView.frame.size.width, 30)];
label.text = #"Test";
label.textAlignment = UITextAlignmentCenter; // DEPRECATED IN iOS 6.0
label.backgroundColor = [UIColor redColor];
[brandView addSubview:label];
[self.scrollView addSubview:brandView];

How can I read the coordinates of a button?

UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background.jpg"]];
self.view = contentView;
[contentView release];
[contentView addSubview:scrollView];
scrollView.contentSize = CGSizeMake(1800,480);
UIScrollView* tscrollView = [[UIScrollView alloc] initWithFrame:(CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = 320.0f, .size.height = 5480.0f}];
tscrollView.contentSize = CGSizeMake(1800,192);
Object1 = [UIButton buttonWithType:1];
Object1.frame = CGRectMake(383, 250, 256, 192);
UIImage *buttonImage = [UIImage imageNamed:#"02_1024_768.jpg"];
[Object1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
//[self.view addSubview:Object1];
[scrollView addSubview:Object1];
Object5 = [UIButton buttonWithType:1];
Object5.frame = CGRectMake(1487, 250, 256, 192);
UIImage *buttonImage1 = [UIImage imageNamed:#"p5.jpg"];
[Object5 setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[scrollView addSubview:Object5];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 100, 200, 200)];
[myLabel setBackgroundColor:[UIColor clearColor]];
myLabel.text = #"Button1 is in range";
[myLabel setTextAlignment:UITextAlignmentCenter];
[myLabel setTextColor:[UIColor whiteColor]];
NSLog(#"x = %d",Object1.center.x);
position = CGPointMake(0,0);
Object1.center = CGPointMake(Object1.center.x+position.x,Object1.center.y+position.y);
if((Object1.center.x >341) && (Object1.center.x < 597)){
[myLabel setHidden:NO];
}
else {
[myLabel setHidden:YES];
}
[self.view addSubview:myLabel];
Here is the code, but seems that the x coordinates always equals to 0. Could some1 tell me why and how to fix it? Thanks in advance.
Try this
NSLog(#"x=%f",Object1.center.x);
coordinates are float value and you print like int so you get 0. i check your code in project

Showing a Title and an Image Into the Table Header

I am trying to show into the header of my UITableViewController a title and under it an image illustrating the title. The next code (written into the viewDidLoad method) shows only the image, and this image over the rest of the table sections. How I can fix it to do what I want?
// Creates a header view.
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
headerLabel.text = self.name;
headerLabel.textColor = [UIColor blackColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.backgroundColor = [UIColor clearColor];
GRect imageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *headerImage = [[UIImageView alloc] initWithFrame:imageRect];
UIImage *image = [UIImage imageNamed:self.imagePath];
[headerImage setImage:image];
headerImage.opaque = YES;
[containerView addSubview:headerLabel];
[containerView addSubview:headerImage];
self.tableView.tableHeaderView = containerView;
Thanks for reading.
Just adjust the views' frames accordingly. The container view's frame must be tall enough to contain the label and image view, and the image view's y coordinate must be set so that it is positioned below the label. The frame rects you are using in your code do not match.