Issue with "autoresizingMask" with two UITextViews - ios

I have a problem with "autoresizingMask" with two UITextView. When I resize the two UITextViews, overlaps the first above the second.
I attached an image to explain the problem.
Thanks!
Update: I put the revenant code
[UIView]
self.autoresizesSubviews = YES;
// Configure title label
_titleLabel = [[UILabel alloc] init];
_titleLabel.frame = CGRectMake(4.0, 4.0, self.bounds.size.width - 8.0, 33.0);
_titleLabel.backgroundColor = APP_COLOR(1.0);
_titleLabel.font = FONT_LATO_BOLD(18.0);
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:_titleLabel];
// Question textView
_questionTextView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(_titleLabel.bounds), (usableHeight / 2.0) - 4.0)];
_questionTextView.layer.cornerRadius = 8.0;
_questionTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
[self addSubview:_questionTextView];
// Answer textView
_answerTextView = [[UITextView alloc] initWithFrame:CGRectMake(CGRectGetMinX(_questionTextView.frame), CGRectGetMaxY(_questionTextView.frame) + 8.0, CGRectGetWidth(_questionTextView.bounds), (usableHeight / 2.0) - 4.0)];
_answerTextView.layer.cornerRadius = 8.0;
_answerTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
[self addSubview:_answerTextView];

The autoresize is working fine.
The two views just don't fit together in the parent view.
You have to adjust the view height manually.

Related

Create UIView programmatically with Objective-C to work in both portrait and landscape

I've to create a view in objective c with two labels on it, label1 has single line and label2 is multi line based on content.
Based on content in the labels I want to adjust view height how can I do that?
Width of the view should be 20 left and right to screen width with below code I can show in portrait but in landscape it was not coming properly, It crops on the right side. How can I do show that right 20 for landscape?
Portrait
Landscape
UIView *emptyTreeView = [[UIView alloc] initWithFrame:CGRectMake(20,20,self.view.frame.size.width - 40,400)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 30.0, self.view.frame.size.width - 100, 30.0)];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 90.0, self.view.frame.size.width - 100, 100.0)];
emptyTreeView.backgroundColor=[UIColor blueColor];
label1.backgroundColor = [UIColor redColor];
label1.textAlignment = NSTextAlignmentCenter;
label1.textColor = [UIColor blackColor];
label1.font = [UIFont boldSystemFontOfSize:18];
label1.numberOfLines = 0;
label1.text = #"The Page Cannot be displayed.";
label2.backgroundColor = [UIColor whiteColor];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor grayColor];
label2.font = [UIFont systemFontOfSize:15];
label2.numberOfLines = 0;
label2.text = #"Please use this feature or contact your internal contact person directly.";
[emptyTreeView addSubview:label1];
[emptyTreeView addSubview:label2];
[self.view addSubview:emptyTreeView];
Am I doing anything wrong?
A few observations:
Do not add subviews in viewDidLayoutSubviews. That is only for adjusting frame values.
If you refer to self.view, reference its bounds, not its frame. The former is for coordinates of subviews within self.view. The latter is in the coordinate system of its superview (which might not introduce any differences right now, but is just the wrong coordinate system.
If you want the view to resize on rotation, set its autoresizingMask, e.g.
emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
Or
emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
Nowadays, setting frame values and autoresizing masks is a bit of an anachronism. We would generally use constraints, e.g.
- (void)viewDidLoad {
[super viewDidLoad];
UIView *emptyTreeView = [[UIView alloc] init];
UILabel *label1 = [[UILabel alloc] init];
UILabel *label2 = [[UILabel alloc] init];
emptyTreeView.backgroundColor = [UIColor blueColor];
label1.backgroundColor = [UIColor redColor];
label1.textAlignment = NSTextAlignmentCenter;
label1.textColor = [UIColor blackColor];
label1.font = [UIFont boldSystemFontOfSize:18];
label1.numberOfLines = 0;
label1.text = #"The Page Cannot be displayed.";
label2.backgroundColor = [UIColor whiteColor];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor grayColor];
label2.font = [UIFont systemFontOfSize:15];
label2.numberOfLines = 0;
label2.text = #"Please use this feature or contact your internal contact person directly.";
[emptyTreeView addSubview:label1];
[emptyTreeView addSubview:label2];
[self.view addSubview:emptyTreeView];
emptyTreeView.translatesAutoresizingMaskIntoConstraints = false;
label1.translatesAutoresizingMaskIntoConstraints = false;
label2.translatesAutoresizingMaskIntoConstraints = false;
[NSLayoutConstraint activateConstraints:#[
[emptyTreeView.leftAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leftAnchor constant:20],
[emptyTreeView.rightAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.rightAnchor constant:-10],
[emptyTreeView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:20],
[label1.leftAnchor constraintEqualToAnchor:emptyTreeView.leftAnchor constant:20],
[label1.rightAnchor constraintEqualToAnchor:emptyTreeView.rightAnchor constant:-20],
[label1.topAnchor constraintEqualToAnchor:emptyTreeView.topAnchor constant:20],
[label2.leftAnchor constraintEqualToAnchor:emptyTreeView.leftAnchor constant:20],
[label2.rightAnchor constraintEqualToAnchor:emptyTreeView.rightAnchor constant:-20],
[label2.topAnchor constraintEqualToAnchor:label1.bottomAnchor constant:20],
[emptyTreeView.bottomAnchor constraintEqualToAnchor:label2.bottomAnchor constant:20]
]];
}
Note the translatesAutoresizingMaskIntoConstraints is false and the constraints.

UIView Programmatic Width Constraint

What kind of constraint(s) do I need to add in order to ensure when the phone rotates the search bar and label span the width of the phone?
Below is code I am using to spin up a search bar & label and add them to a table header:
self.venueSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.venueSearchController.searchResultsUpdater = self;
self.venueSearchController.searchBar.delegate = self;
self.venueSearchController.delegate = self;
self.venueSearchController.dimsBackgroundDuringPresentation = NO;
self.venueSearchController.hidesNavigationBarDuringPresentation = YES;
self.venueSearchController.searchBar.frame = CGRectMake(0, 0, self.venueSearchController.view.frame.size.width, 44.0);
// create a UISegmentControl
UILabel *tableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, self.venueSearchController.view.frame.size.width, 30)];
[tableHeaderLabel setTextAlignment:NSTextAlignmentCenter];
tableHeaderLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
tableHeaderLabel.text = #"Text Here";
tableHeaderLabel.backgroundColor = [UIColor redColor];
// create a custom UIView
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, 74)];
[myView addSubview:tableHeaderLabel]; // add header label
[myView addSubview:self.venueSearchController.searchBar]; // add search bar to custom view
self.tableView.tableHeaderView = myView;
FYI I fixed this by simply using autoresizingMask
tableHeaderLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.venueSearchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

Remove shadow from placeholder text in UITextField styled using CALayer

I've created a UITextField from the Interface Builder(IB).
Since I wanted custom styling, I selected the None styling in IB.
On viewDidLoad, I'm assigning it the following style:
txtEmail.layer.cornerRadius = 8.0;
txtEmail.layer.borderColor = [UIColor colorWithWhite:199.0/255.0 alpha:1].CGColor;
txtEmail.layer.borderWidth = 1.0;
txtEmail.layer.shadowColor = [UIColor blackColor].CGColor;
txtEmail.layer.shadowOpacity = 0.17;
txtEmail.layer.shadowOffset = CGSizeMake(-0.9,0.9);
txtEmail.layer.shadowRadius = 1.1;
// This is to provide a left padding for None styled textfields
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
txtEmail.leftView = paddingView;
txtEmail.leftViewMode = UITextFieldViewModeAlways;
Now my textfield is getting perfectly styled, but my placeholder text (provided in IB) is also inheriting that shadow. How can I get rid of that?
Find the below code its working fine, you need to remove shadowRadius
txtEmail.backgroundColor = [UIColor clearColor];
txtEmail.layer.cornerRadius = 8.0;
txtEmail.layer.borderColor = [UIColor colorWithWhite:199.0/255.0 alpha:1].CGColor;
txtEmail.layer.borderWidth = 1.0;
txtEmail.layer.shadowColor = [UIColor blackColor].CGColor;
txtEmail.layer.shadowOpacity = 0.17;
txtEmail.layer.shadowOffset = CGSizeMake(-0.9,0.9);
txtEmail.layer.masksToBounds = false;
// This is to provide a left padding for None styled textfields
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
txtEmail.leftView = paddingView;
txtEmail.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:txtEmail];

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;

iOS: UILabel centered in view

I have a UISplitViewController and am trying to center (horizontal and vertically) a UILabel in the right view controller. When it loads in portrait the label looks correct, but if you rotate it to landscape the label is no longer centered vertically. I am using:
CGSize size = self.view.frame.size;
UILabel *noResults = [[UILabel alloc] initWithFrame:CGRectMake(0, 40.0f, size.width, size.height)];
noResults.text = #"No people were found.";
noResults.textColor = [UIColor blackColor];
noResults.textAlignment = UITextAlignmentCenter;
noResults.backgroundColor = [UIColor clearColor];
noResults.textColor = [UIColor darkGrayColor];
noResults.font = [UIFont systemFontOfSize:16.0];
noResults.shadowColor = [UIColor whiteColor];
noResults.shadowOffset = CGSizeMake(0, 1);
noResults.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.noResultsLabel = noResults;
[self.view addSubview:noResultsLabel];
[noResults release];
I thought it would automatically resize itself when I used a autoresize mask?
You should also specify that the margins are flexible, like this:
noResults.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
By using this code, you will get the button repositioned horizontally. If you also want it to be repositioned vertically, also specify UIViewAutoresizingFlexibleTopMargin and UIViewAutoresizingFlexibleBottomMargin.

Resources