Button over tableviewcontroller stretching (Swift)? - ios

I have a button in a view which is in the footer of a tableview (UITableViewController). Why is the button stretching when I try to apply the following code to it?
And I apply the code:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Make footerview so it fill up size of the screen
// The button is aligned to bottom of the footerview
// using autolayout constraints
self.tableView.tableFooterView = nil
self.footerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.tableView.frame.size.height - self.tableView.contentSize.height - self.footerView.frame.size.height)
self.tableView.tableFooterView = self.footerView
}
I was following the answer on this question:
Add button on top of UITableViewController (Swift)
Thanks!

If you read the question (whose link you posted) carefully, you can see that he resizes the view to take up the rest of the screen that is remaining after your table. Your button is the same size as your view, that is why it is stretching up. You need to add constraints which bind your button to the bottom of the view but not the top. Because if you bind the top and the bottom both to the view it will stretch.
Here is the example.
1. When you do not bind the button to the top. Notice that in the constraints, I do not have any constraint that specify the top of the button.
When you bind your button to the the top of the view. In this, I set a constraint which bind the button top to the view top. It stretches my button to take up the whole space as the view(which is similar to your case)
Hope this helps. :)

Related

Replace view inside UIStackView

I have a UIStackView with 5 buttons inside. One of those buttons (#4) needs to be replaced by a UIPickerView once it is clicked and switched back to the UIButton once one of the other four's is selected.
The UIStackView is horizontal, alignment is fill and distribution is Fill Proportionally.
The stack was done using storyboard, and I am trying to replace the view with:
override func viewDidLayoutSubviews(){
self.pickerView.frame.origin = self.view.viewWithTag(40)!.frame.origin
self.pickerView.frame.size = CGSize(width: 30, height: 30)
}
But my UIStackView will not rearrange after the button click, and button #5 is staying on top of the UIPickerView.
Any ideas?
Ok, I found the code. I had to replace addView for addArrangedSubview.
Final code (on button press):
let stack = (self.view.viewWithTag(20) as! UIStackView)
stack.addArrangedSubview(self.pickerView)

IOS/Objective-C: MakeUILabel Flow to Multiple Lines with Autolayout

I am trying to get a UILabel to flow to multiple lines and push the elements below it downwards using autolayout.
Here is my code called in viewwillappear.
self.myLabel.numberOfLines = 0;
self.myLabel.lineBreakMode = NSLineBreakByWordWrapping;
[_myLabel setContentCompressionResistancePriority:1000
forAxis:UILayoutConstraintAxisVertical];
[self.myLabel setNeedsDisplay];
[self.myLabel layoutIfNeeded];
[self.view setNeedsDisplay];
[self.view layoutIfNeeded];
The UILabel is constrained at the top to the content view and the image below it is constrained to the bottom of the UILabel. The label also has a height constraint of >=21.
On initial load, the label only shows one line.
The weird thing is that after launching a modal VC and the closing it, the label does go to multiple lines but fails to push down the elements below it.
On initial load.
After launching and canceling modal VC
I am wondering if the problem has something to do with timing of laying out subviews but have tried almost every thing.
Thanks in advance for any suggestions.
Simple example:
Create a new ViewController.
Add a button, constrain 40-pts from the top and centered horizontally
Add a UIView (green view). Constrain centered horizontally, width of 240, and top-space to button of 20.
Add two labels to the green view.
Top label, number of lines = 0, leading and trailing constraints of 16, top constraint of 8 (to green superview)
Bottom label, centered horizontally, top space to Top label of 8, bottom constraint of 8 (to green superview).
Connect the Top label to #IBOutlet var multiLineLabel: UILabel! in the view controller.
Connect the button touch-up-inside to the #IBAction func didTap(_ sender: Any) function in the view controller.
NO CHANGES to priorities.
Run the app. Each tap of the button will add text to the top / multi-line label, and it will "push down" the bottom label, which will, in turn, "push down" the bottom edge of the green view.
class ExpandingLabelViewController: UIViewController {
#IBOutlet var multiLineLabel: UILabel!
#IBAction func didTap(_ sender: Any) {
if let s = multiLineLabel.text {
multiLineLabel.text = s + " Here is some more text."
}
}
}
Once you have that working, you can add other elements to the view. Just make sure you have a "chain" of vertical spacing constraints, with the top-most element constrained to the top of the green superview, and the bottom-most element constrained to the bottom of the green superview.

Have UIView extend to the bottom of UIScrollView

I put a UIScrollView occupies the whole area of my controller's view. Then I added a UIView (yellow color) on UIScrollView:
In my controller code, I have set the height of my scroll view:
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.contentSize.height = 1000
}
When I run it and scroll up on the screen, I see this:
How to make the yellow view component's bottom matches the bottom of scroll view?
From the information provided the only guess could be that you forgot to add layout constraints.
In case you did add them, please update the question showing your constraints.

How to show tableView to top when scrolled

In my project,
In the view 'Top' side is for one UIView and below it a tableView. I want to show tableView to top when scrolled and hide the tableView.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.topView.isHidden = true }
Please give me solution for how to show tableView to top when tableView scrolled.
Below is the screenshot
[![enter image description here][1]][1]
in this view bottom is tableView and top is UIView.
Below is the storyboard screenshot.
The simplest solution is to set that topView as tableHeaderView.
self.tableView.tableHeaderView = topView
Now when you scroll the tableView topView will goes up with it. Also no need to implement scrollViewDidScroll now.
Select your tableview in your Controller of Storyboard
Select your TableView
Go to Attributes Inspector on the right panel of Utilities
Set Style to Grouped

Pin Footer/View to bottom of screen programmatically with AutoLayout

I have a UITableView, but I would like an ever present UIView at the bottom of the TableViewController screen.
I want to use AutoLayout programmatically to pin this view to the bottom and sides of the screen, because it needs to work with multiple screen sizes.
I ran into problems trying to add a footer view to the UITableView because I wasn't able to pin it to the bottom of the screen (edge cases where there is only 1 item in the UITableView would render the footer in the middle of the screen.
Is there any way to do that well?
I've started with this, but I'm stuck trying to figure out what to put for the frame since the frame will change based on screen size:
let bottomView = UIView()
bottomView.backgroundColor = .black
view.addSubview(bottomView)

Resources