UISearchBar covered by section header - ios

I have a searchBar showing as part of the tableViewHeader as created in the storyboard. However I would like to hide it under some circumstances. To accomplish this, I set the tableHeaderView.frame to a height of 0. To show it again, I set the tableHeaderView.frame to its original height. When it does show it is covered by the first section header(s). Pulling down moves the search bar along with the section header.
I tried [self.searchBar sizeToFit] after it is shown without any success. I also tried [self.view layoutSubviews].
How do I keep the section title from covering the search bar? (iOS 6 and iOS 7)

Instead of hiding it by setting its height to zero, you can set the table's content offset to the search bar height. This makes it basically scroll up out of view.
Something like this:
#property (nonatomic, weak) IBOutlet UISearchBar* searchBar;
#property (nonatomic, weak) IBOutlet UITableView* table;
...
- (void) hideSearchBar
{
self.table.contentOffset = CGPointMake( 0, self.searchBar.frame.size.height );
}

Related

How to set hide and show view using constraints like auto layout /StackView

I have a larger screen to design, it had plus button, for adding more fields in it. For example, i need to add 2 Email/Phone number, when i press plus button it will show one more textfield with in Email header as vice versa. If i use hide and show method programmatically, the rest of the space will be blank. For this i need to set constraint and setLayout for view. How is it possible?
I would like to give idea so quick look at it.
Take a scroll view inside take one view that having all field your textfield like email and more text that you want.
Now design all field whatever you have to show.
add
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *ViewHeightConstraints;
for container view when you hide textfield just subtract height of all hideTextfield to ViewHeightConstraints.
ok when you hide textfield do something like this:
ViewHeightConstraints.constant = 0;
_addressView.hidden = YES;
[self.view layoutIfNeeded];
ok when you Show textfield do something like this:
ViewHeightConstraints.constant = 243;
_addressView.hidden = NO;
[self.view layoutIfNeeded];
whenever you change constraints write this Everytime.
[self.view layoutIfNeeded];
for setting frame for your main View Set height constraints for MainView. i have done like this.
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *containerHeightConstraints;
_containerHeightConstraints.constant = _bottomView.frame.origin.y+_bottomViewHeight.constant;
[self.view layoutIfNeeded];
Hope it will work.
You need to take a IBOutlet of the field height constraint for this.
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *fieldConstraintHeight;
Now when you want to hide it use
fieldConstraintHeight.constant = 0;

Xcode storyboard with dynamic views

I have a problem about Xcode storyboard and dynamically showed views.
I have 3 vertically aligned views in my storyboard and each one is linked with each other with top-bottom relationship with constraints
However during runtime I want to hide the 2nd view and replace its place with the 3'rd view (which is a tableview) and to fill the both 2nd and 3rd places I'm trying to extend the height of tableview.
However I cannot succeed. I have tried lots of things but closest thing I get is to transform the 3rd view to 2nd place but the height remains the same
My latest code is in below
if (status) {
self.filterView.hidden = NO;
self.contentTable.frame = contentTableFrame;
} else {
self.filterView.hidden = YES;
CGFloat predictedHeight = self.contentTable.frame.size.height+(self.contentTableFrame.origin.y-self.filterView.frame.origin.y);
self.contentTable.transform = CGAffineTransformMakeTranslation(0, self.filterView.frame.origin.y-self.contentTable.frame.origin.y);
for (NSLayoutConstraint *constraint in self.contentTable.constraints) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
constraint.constant = predictedHeight;
}
}
}
You can also find the screenshot of the 3rd view's constraints. What should I do to fix that? Does anyone have any idea about it?
I also have another solution to this problem . But I've another problem on that too.
When I executed the below line of code my 3rd view move to 2nd view's place but its height remains the same so on the bottom of the page it happens to seems a blank space.
self.contentTable.transform = CGAffineTransformMakeTranslation(0, self.filterView.frame.origin.y-self.contentTable.frame.origin.y);
In order to fix that I've tried to change its height(with .frame = CGRectMake(.....) )
but it didn't work.Then I've tried to scale the 3rd view(which is a tableview) and it succeed but because I've scaled it all the cells inside the tableview scaled too and the visual appearance of the table has broken. So I couldn't able to find a solution to that problem.
It seemed like a challenge.
Thanks
Sorry it is in objective-c.
I get the top contraint from the TableView (Storyboard). When I hide the topView, I get the height of my topView and change the top constraint constant. You can see it in #IBAction. Finally, the tableView is stretched and takes the space left from the topView. Is that what you were looking ?
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UIView *topView;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *topTableViewConstraint;
- (IBAction)pushTest:(id)sender;
#end
ViewController.m
- (IBAction)pushTest:(id)sender {
if (self.topView.hidden) {
self.topTableViewConstraint.constant = 0;
}else{
self.topTableViewConstraint.constant = -self.topView.bounds.size.height;
}
self.topView.hidden = !self.topView.hidden;
}
you can hide filterview but you can't replace it by tableview.
if you want to replace it by table view.You need to use method BringSubviewToFront for That Tableview.it will bring that tablview to front and set filterview to back which is hidden for ui.

Why do I fail to set the frame size of a UITableView?

I have a UIViewController with a UITableView set in interface builder.
In my h-file, I have connected the UITableView *myTableView.
(#property (strong, nonatomic) IBOutlet UITableView *tableView;)
In the bottom of my view, I have a toolbar. This toolbar covers the last cell of myTableView.
After reading around a lot, I find that I can set the height this way:
CGRect fr= CGRectMake(0, 0, myWidht, myHeight);
[self.myTableView setFrame:fr];
NSLog(#"test %f",self.myTableView.frame.size.height);
This logs : test "myHeight", but myTableView is still covered by the toolbar!!!
I have also tried to set the constraints in interface builder, but I still fail.
Is there a way to fix this issue, so the last cell in the tableview is not covered by the toolbar in the bottom of the view?
Resetting the simulator, and adjusting constraints in interfacebulder solved it :-)

UITableViewCell subclass layout messed up in autoLayout-disabled Storyboard

I have created a table view with prototype cells in a Storyboard with the "Use Autolayout" unchecked in Xcode 5.
The cells are UITableViewCell subclasses, mainly to add IBOutlets and no code inside. The result is a messed layout. I tried changing the Autoresizing masks with no luck. Also tried this.
If I however implement an empty layoutSubviews it shows ok. Any idea of what's going on?
Is auto layout still enabled despite unchecking it?
Edit:
More details...
#interface SettingDefaultTableViewCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *label;
#property (nonatomic, weak) IBOutlet UIImageView *imageView;
#property (nonatomic, weak) IBOutlet UIView *backgroundView;
#end
#implementation SettingDefaultTableViewCell
- (void)layoutSubviews
{
// Emtpy implementation to fix weird layout bug
}
#end
Storyboard with Autolayout disabled:
Result when layoutSubviews fix above is not used:
Edit 2:
The cell is set to not autoresize subviews.
While the above should be enough to prevent subviews from getting auto-adjusted, also all autoresizing masks are set to flexible right and bottom margin only.
Only the > mark is set to flexible left margin. No flexible width or height.
The cell is getting modified by the undocumented UITableViewCell's layoutSubviews:
Despite set not no autoresize subviews, the default implementation changes the backgroundView frame.
It also moves imageView (the ">") to the left despite being configured as a Custom Style cell.
Thus avoiding executing super's layoutSubviews fixes it.

UIView animated changes with auto layout constraints

Say that I have a project which looks like the following:
There are two UIViews - one called yellowBox and the other called redBox. The auto layout constraints dictate that the yellowBox is 60 points from the top of the superview with 350 points leading and trailing spaces (i.e. to left and right of the view). The redBox has the same leading and trailing space constraints. There is a vertical space constraint between the two boxes of 0 to indicate that the bottom of the yellowBox should always be directly on top of the redBox.
When the user taps the Expand Yellow Box button, I would like the height of the yellowBox to increase and, as a result, the redBox moves down to continue satisfying the vertical space constraint (that the yellowBox is always on top of the redBox). Here is the animation code:
- (IBAction)expandYellowBox:(id)sender {
[UIView animateWithDuration:0.5
animations:^{
CGRect newFrame = self.yellowBox.frame;
newFrame.size.height += 50;
self.yellowBox.frame = newFrame;
}];
}
However, I have been unable to get this working properly. As you can see by the red icon, there is a problem with the constraints at the moment:
which is fair enough, as there's no way for auto layout to know the height of the boxes. However, I don't know how to resolve this issue in such a way that it will allow for the boxes to be resized and moved. For example, if I specify a height constraint on the yellowBox then that will prevent it from being resized. If I set the height constraint to be greater than or equal (to allow the yellowBox height to increase) then I get a different constraint error:
All constraints have been established using Xcode in the storyboard - none have been written in code.
Any help greatly appreciated.
EDIT: As it turns out, the yellowBox is growing when the button is clicked - it's just I couldn't tell because it appears behind the redBox. I only noticed after clicking it around four times and it started appearing out the bottom of the redBox. However, the same question still remains - how to get the redBox to always stick to the bottom of the yellowBox.
Try it as mentioned by shwet-solanki, but add the following line after changing the constraint:
[self.view layoutIfNeeded];
the IBAction would look like:
- (IBAction)expandYellowBox:(id)sender {
[UIView animateWithDuration:0.5
animations:^{
self.yellowBoxHeightConstraint.constant += 50;
[self.view layoutIfNeeded];
}];
}
Where yellowBoxHeightConstraint is the IBOutlet for height constraint of yellowBox.
Hope this helps.
Add Height constraint to both the views
Create an IBOutlet for height constraint of yellowbox
Now instead of changing the height of yellowbox in the button pressed event, rather change the value of the height constraint. i.e suppose your IBOutlet constraint name is yellowBoxHeightConstraint, then yellowBoxHeightConstraint.constant += 50;
Hope this works for you.
//
// WPViewController.h
// AutoLayoutTEst
//
// Created by VASANTH K on 09/01/14.
//
//
#import <UIKit/UIKit.h>
#interface WPViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *button1;
- (IBAction)buttClicked:(id)sender;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstrain;
#property (strong, nonatomic) IBOutlet UIButton *button2;
#end
click Event
- (IBAction)buttClicked:(id)sender {
self.heightConstrain.constant=100;
}
here u need to set the heightConstrain for the Yellow button then create reference outlet for that button then update the heightConstrain to update the size of that button it will automatically move your Red button down.
https://github.com/vasanth3008/AutoLayoutHeighDemo
refer my sample demo project

Resources