Strange behaviour when laying out subviews - objective-c

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];

Related

objective-c ios auto layout NSLayoutConstraint

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.

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)];

Cannot change the Y cordinate of the UILabel programmatically

hi Im programmatically creating an UILabel like this
`
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[[UILabel alloc] init] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)];
// _titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
_titleLabel.backgroundColor =[UIColor clearColor];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
_titleLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:_titleLabel];
}
return _titleLabel;
}`
this _photoView is an UIImageView I have created already. I want to change the UILabel View Y value. But the problem is when I change this second parameter label y position is not changing. Any one can tell e the reason for this.
And this is how I created the ImageView
`
- (UIImageView *)photoView {
if (!_photoView) {
_photoView = [[UIImageView alloc] init];
_photoView.contentMode = UIViewContentModeScaleAspectFill;
_photoView.clipsToBounds = YES;
_photoView.layer.cornerRadius = 5;
_photoView.clipsToBounds = YES;
[self addSubview:_photoView];
}
return _photoView;
}`
Thanks
In this line , you have used two types init
[[[UILabel alloc] init] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)]
change this line to
[[UILabel alloc] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)]

text format trouble objective-c

need to make effect, that text is interrupted by another text/uiimageview, but cannot understand how does it works. For example i need to make interface similar to ios7 status bar where operator name such a "Oper ..." + icon + time. So i cannot do this right way
operatorName = [self getOperatorName];
UILabel *operatorLabel = [[UILabel alloc] init];
operatorLabel.backgroundColor = [UIColor clearColor];
operatorLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:kStatusBarFontOperatorSize];
operatorLabel.textColor = kStatusBarTextColor;
operatorLabel.shadowOffset = CGSizeMake(0.f, -0.6);
operatorLabel.shadowColor = kStatusBarTextShadow;
operatorLabel.adjustsFontSizeToFitWidth = YES;
operatorLabel.text = operatorName;
operatorLabel.frame = CGRectMake(0.0, 0.0, operatorStrSize.width, operatorStrSize.height);
[operatorLabel sizeToFit];
/* connection type */
UIImageView *conImgView = [[UIImageView alloc] initWithImage:conImg];
/* time in status bar */
time = [self getStatusBarTime];
UILabel *statusBarLabel = [[UILabel alloc] init];
statusBarLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:kStatusBarFontSize];
statusBarLabel.textColor = kStatusBarTextColor;
statusBarLabel.adjustsFontSizeToFitWidth = YES;
statusBarLabel.text = time;
int maxDistance = imgView.frame.size.width/2 - timeStrSize.width/2;
int connectionPower = 44;
double delimiter = 0;
NSString *cName = [self returnChoosenConnectionName];
if ([cName isEqualToString:#"Wi-Fi"]) {
delimiter = 6.5;
} else {
delimiter = 9.5;
}
int fullLine = connectionPower + operatorLabel.frame.size.width + delimiter + conImgView.frame.size.width;
if (fullLine > maxDistance) {
// need to interrupt text in operator name, but how ?
} else {
// all good placed
x = 44.0;
operatorLabel.frame = CGRectMake(x, 3.0, operatorStrSize.width , operatorStrSize.height);
[operatorLabel sizeToFit];
NSString *cName = [self returnChoosenConnectionName];
if ([cName isEqualToString:#"Wi-Fi"]) {
x += operatorLabel.frame.size.width + 6.5;
} else {
x += operatorLabel.frame.size.width + 9.5;
}
conImgView.frame = CGRectMake(x, 0.0, conImgView.frame.size.width, conImgView.frame.size.height);
[imgView addSubview:operatorLabel];
[imgView addSubview:conImgView];
}
statusBarLabel.frame = CGRectMake(imgView.frame.size.width/2 - timeStrSize.width/2, 2.5, timeStrSize.width , timeStrSize.height);
[imgView addSubview:statusBarLabel];
What i need:
what i have:
I would suggest to use autolayout rather than trying to calculate everything yourself. You may use XIB and set constraints in there or you may do everything programmatically. Here is a sample code that will get you started. It creates controls and sets constraints programatically; it doesn't use XIB.
- (void)viewDidLoad
{
[super viewDidLoad];
/*
Uncomment this if you would like to translate your subviews vertically.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 50)];
[self.view addSubview:view];
UIView *superview = view;
*/
UIView *superview = self.view;
UILabel *label1 = [[UILabel alloc] init];
label1.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:14.0];
label1.text = #"MTS";
[label1 sizeToFit];
[label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
UILabel *label2 = [[UILabel alloc] init];
label2.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:14.0];
label2.text = #"1:22 PM";
[label2 sizeToFit];
[label2 setTranslatesAutoresizingMaskIntoConstraints:NO];
UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strength.png"]];
UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"carrier.png"]];
[image1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[image2 setTranslatesAutoresizingMaskIntoConstraints:NO];
[image1 sizeToFit];
[image2 sizeToFit];
[superview addSubview:image1];
[superview addSubview:label1];
[superview addSubview:image2];
[superview addSubview:label2];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(image1, image2, label1, label2);
NSString *format = [NSString stringWithFormat:#"|-[image1(<=%f)]-[label1]-[image2(<=%f)]-[label2(>=20)]-|",
image1.frame.size.width, image2.frame.size.width];
NSArray *constraints;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
}
When adding views to a layout in code iOS will attempt to convert the autosizing mask for that view to auto layout constraints. Those auto-generated constraints will conflict with any constraints added within the application code. It is essential to turn the translation off:
setTranslatesAutoresizingMaskIntoConstraints:NO
The result of the code is:

Can I set frame for UIView after adding subviews to that UIView?

I need UIView with dynamic size (it can containg 1 or 2 labels) and I do something like this:
float nextY = 0.0f;
float labelHeight = 20.0f;
UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, nextY, 100.0f, labelHeight)];
label1.text = #"Test 1";
[tempView addSubview:label1];
nextY += labelHeight;
if (something == YES)
{
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, nextY, 100.0f, labelHeight)];
label1.text = #"Test 2";
[tempView addSubview:label2];
nextY += labelHeight;
}
tempView.frame = CGRectMake(0.0f, 0.0f, 100.0.f, nextY);
I that ok?
What you are doing is valid. If you set the autoresizing mask on the subviews, then when you change the superview's frame, the system will also adjust the frames of the subviews.