I want to change UITableView height depending upon user interaction with a button. When user clicks on the button, UITableView gets expanded and click on the same button UITableView height is decreased only to show 1st row. Following is my code
tblFaults?.setNeedsUpdateConstraints()
self.view.setNeedsUpdateConstraints()
if (currentNumber == 1)
{
currentNumber = 2
var newFrame = tblFaults?.frame
newFrame?.size.height += 67;//tried to change frame as well
tblFaults?.frame = newFrame!
faultViewHeight?.constant = 115
//faultView height is tableview height constraint
}
else
{
currentNumber = 1
var newFrame = tblFaults?.frame
newFrame?.size.height -= 67;
tblFaults?.frame = newFrame!
faultViewHeight?.constant = 48
}
tblFaults?.reloadData()
//self.view.setNeedsLayout()
//self.view.setNeedsDisplay()
I tried setNeedsDisplay, setNeedsLayout as well as layoutSubviews() after the code. But view does not get refreshed immediately after button click but gets refreshed only after next user interaction (e.g. scrollview change etc)
Try this :
faultViewHeight?.constant = 48
tblFaults.layoutIfNeeded() // you need to call this method
Thanks everyone.
Actually only constraint constant value change and tableview.reloadData() was enough. The BIG mistake I did was, I was checking the execution on simulator. I tried this on device and it worked perfectly fine. On simulator I was facing the issue even after trying self.view.layoutIfNeeded(). I still do not know why this is the behaviour on simulator and not on device. But eventually app has to run on device and its working there. :)
Thank you all.
Related
I've got some square buttons that I'd like to add rounded corners to that are proportional to the button's height. In past versions of my app, I had implemented this feature without issues using viewDidLayoutSubviews(). For some reason, after pushing a new version of my app with other features I had tweaked, this section of code no longer functions as expected. Here is the code:
override func viewDidLayoutSubviews() {
for button in buttons {
button!.layer.shadowColor = UIColor.black.cgColor
button!.layer.shadowOffset = CGSize(width: 0, height: 1.0)
button!.layer.shadowOpacity = 0.4
button!.layer.shadowRadius = button!.frame.height / 40
button!.layer.cornerRadius = button!.frame.height / 10
}
Again, this block of code used to work just fine but for some reason it no longer works. What I am experiencing is much larger relative radii on smaller buttons (iPhone SE) compared to bigger buttons (iPads).
To troubleshoot, in viewDidLayoutSubviews(), I'm printing the button!.frame.height and I'm noticing that no matter what device I use the frame height is 395.5, which I believe is the correct size only on the 12.9" iPad. Therefore, the buttons look correct on the 12.9" iPad but the radii end up being too large on all of the smaller devices.
Any idea what's going on here? Why is it that they're all returning the same frame height even though they're visually very different sizes on the different devices?
I copy and pasted the above code into the viewWillAppear() method and
the problem was resolved. I then deleted the code from
viewWillAppear(), leaving me with my original code during posting of
question, and it is continuing to run as expected (working). What
could possibly be the cause of this intermittent behavior
The reason when you initialized the buttons in viewWillAppear and remove them but it still work because your button's frame did not change in the viewDidLayoutSubview method. And the viewDidLayoutSubview is invoked only controller's view is updated, rotated, or changed, which in your case it does not.
If you try to rotate your device you will see your parent view's frame changed.
For more information about view hierarchy. See this article
Try like this:-
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for button in buttons {
button!.layer.shadowColor = UIColor.black.cgColor
button!.layer.shadowOffset = CGSize(width: 0, height: 1.0)
button!.layer.shadowOpacity = 0.4
button!.layer.shadowRadius = button!.frame.height / 40
button!.layer.cornerRadius = button!.frame.height / 10
}
When I call scrollToItemAtIndexPath method to scroll to bottom with keyboard-on,
collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
I always get this strange animation.
It first scrolls down to the end, and scrolls up again a bit, and scrolls down to the end again. Why this happens and how can I fix this?
I checked this is happening in iOS 9.3 simulator.
UICollectionView shows this behaviour when its contentInsets.top != 0.
I haven't set it explicitly, but it's been set automatically by UIViewController. This can be disabled by setting automaticallyAdjustsScrollViewInsets property.
automaticallyAdjustsScrollViewInsets = false
Then the strange animation magically goes away. Anyway, now my collection view has no top space, so it renders badly.
To make this to work, just set insets manually.
// collectionView.contentInset.top = 20 + 44
collectionView.scrollIndicatorInsets.top = 20 + 44
But if we set contentInset.top to non-zero value, it shows the weird animation again, so you shouldn't set it. Instead of, we should try another workaround. For example, you can adjust UICollectionViewFlowLayout.sectionInset.top if you are using flow layout.
collectionView.scrollIndicatorInsets.top = 20 + 44
let flowLayout = (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)
flowLayout?.sectionInset.top = 20 + 44
Or you can put an empty section. Or whatever.
Now it works properly.
I want to change the width of my label since my swift code.
If possible, do it in a annimation prograssif for a change. Is it possible ?
When I do:
self.lblChoice1.frame.size.width += 50
My exchange label width not ... Why?
Thank you in advance.
EDIT :
The first response is not working.
print(self.lblChoice1.frame.size.width)
self.lblChoice1.frame.size.width += 150
self.lblChoice1.contentMode = UIViewContentMode.Redraw
self.lblChoice1.setNeedsDisplay()
print(self.lblChoice1.frame.size.width)
This code displays me well in my console:
150.0
300.0
But my label size does not change the display ...
You are changing the frame, but you aren't telling the view that it needs to redraw itself. After changing the frame you should then call
self.lblchoice1.setNeedsDisplay()
Although you may need to change the label's content mode to UIViewContentModeRedraw to make sure it redraws.
Although it would be better to use UIView block animation methods to do this.
I dont think you can edit the UILabel's frame directly. You should change entire frame instead.
var lblChoice1Frame = self.lblChoice1.frame
lblChoice1Frame.size.width += 150
UIView.animateWithDuration(0.3) { () -> Void in
self.lblChoice1.frame = lblChoice1Frame
}
So I am currently very new to ios programming and I am having a bit of an issue with my table view. First I call my web API to get the data I required to populate my table View. Using the data I am able to calculate the number of rows and sections that would exist on my table view. After that I calculate the correct height of my table view. I change the height of my table view and reload the table. It looks like this
func tableViewHight(numberOfRows : Int)
{
let sectionHeight = CGFloat(30) * CGFloat(numberOfSections - 1)
//This is the height of all the sections in my tableview put together(except the first section since its height will always be 0)
let tableviewMaximumHeight = UIScreen.mainScreen().bounds.size.height - self.myTableView.frame.origin.y
//Maximum height would be the distance from the y position of my table view, all the way to the bottom of the device.
if(tableviewMaximumHeight <= (cellRowHeight * CGFloat(numberOfRows) + sectionHeight))
{
self.myTableView.frame.size.height = tableviewMaximumHeight
}
else
{
self.myTableView.frame.size.height = cellRowHeight * CGFloat(numberOfRows) + sectionHeight
}
self.myTableView.reloadData()
}
My table view is able to change height and reload data perfectly. The only Problem is that I am not able to reach the bottom of my table view. I do not know what else to do. I have already check that my table view has
myTableView.scrollEnabled = true
myTableView.scrollTop = false
If you guys have any advice, I would appreciate it :).
The problem when you manually set the height of a UITableView is that you'll then also have to manually set its content height.
Use Autolayout instead to set the height.
Have an IBOutlet for an NSLayoutConstraint variable (say, tableHeightConstraint) that sets the height of your table, then, in your code:
tableHeightConstraint.constant = tableviewMaximumHeight
or
tableHeightConstraint.constant = cellRowHeight * CGFloat(numberOfRows) + sectionHeight
I found the main root of my problem. The issued lied in my maximumTableviewHeight variable. The problem was that I would the value for this variable in the view did load and apparently the y position of the my table view would always give zero in the view did load. Once the maximum table view height was resolve. The application worked like a a charm.
I have an issue on iOS8 my textview doensn't scroll correctly like it used to be on all previous versions.
My code is this one :
const BOOL scrollToBottom = (chatTextView.text.length == 0);
chatTextView.text = getStr;
NSUInteger length = chatTextView.text.length;
if (scrollToBottom)
{
chatTextView.selectedRange = NSMakeRange(length, 0);
[chatTextView scrollRangeToVisible:chatTextView.selectedRange];
Do you have an idea were the problem might come from ?
In the previous ios version the scroll was always scrolling to bottom when there was new messages in the chatbox. Now the scroll view always refresh to somewhere in the middle of the chat box. I've tried quick hack like NSMakeRange(length+1000, 0); but it doens't change anything