How to move a table content to the top of the screen? - ios

I am still new to iOS and Swift and I am using Swift4 and Xcode9 to edit an iOS project which is made with a prior versions of Xcode and Swift (Xcode8 and Swift3). In the project there is a TableViewController used. So the problem is when I open the project in Xcode9 and run it, it shows some space on the top. I tried changing y values, but didn't work. My questions are,
How to solve the upper mentioned problem. (There is a question like this in Stack Overflow already. But it's not due to different Xcode version. But I even tried all the suggestions in the answers. But none of them worked for me.)
When there is a text label or something on the top of the table content, I can remove that space, from the top, but it goes to in between that label and the table added. And the label is unable to get a click action or something when it's moved to the top. How to solve that?
Any suggestion/ answer will be highly appreciated. Thank you!

You can try this
if #available(iOS 11.0, *) {
yourTableView.contentInsetAdjustmentBehavior = .never
}

Ok I'm going to attempt to answer your question but I wasn't totally positive since you didn't include any code or screenshots.
Firstly. If you mean that there seems to be a gap between where the tableView starts and where the first cell is displayed, this is correct. You can fix that by doing:
tableView.contentInset = .zero
this means that any content inside the tableView starts goes all the way to the edges.
Now for the label receiving touches. You want to look at user interaction enabled values on the storyboard:
Whenever you have views that stack on top of each other, the view UNDER will not receive touches if the view on top has user interaction enabled. If you want a touch to bleed through another view you either can
1) avoid the issue by not stacking views on top of each other (often unavoidable, like putting a label on a view and wanting the background view to do something)
2) turn off the user interaction on the view on top so the view on bottom gets the touch

tableView.setContentOffset(CGPoint.zero, animated: true)
this code will move

Related

UITableView: Can't find a way to move/align the tableIndex element up

I have a UITableView and I am using a tableIndex, my problem is that when the keyboard comes up, it partially hides the tableIndex.
An acceptable solution would be to move the tableIndex up (like what the Contacts App does) when the keyboard appears, and show the most of it, but can figure out a way to do this. Any ideas of code snippet I can try?
The way to do this is to register to Keyboard Will Show notification and then update the height of your controller's view. You could also embed everything in a scroll View so that once the keyboard rises, your view becomes smaller and scrollable.
It is honestly a bit annoying to set up, so, if you are open to using CocoaPods then IQKeyboardManager is probably your best bet which can be found here: https://github.com/hackiftekhar/IQKeyboardManager

"Print" & "Cancel" disappeared from Printer Options in UIActivityViewController

The buttons are still there and function fine, however as you can see they are completely invisible. Adjusting font colors and navigation bar colors had no effect. I have been trying to resolve this for months and despite reading Apple's documentation, I am unable to even see where you would change what I assumed to be a system wide function.
So to help narrow down this issue I created a simple navigation controller with a print button. If I put these both on the Login storyboard (the first storyboard) it works perfectly fine. If I put it on Main.storyboard (the second storyboard) it doesn't work.
If I change my project settings to make Main.storyboard the first storyboard it works. Leading me to believe the issue is in my FirstViewController.
You can set the color of the print and cancel button
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIToolbar.self]).tintColor = UIColor.green
After narrowing down the problem to the first view controller I discovered the tint was set to Clear Color (ie Alpha is 0). Fixing this resolved the problem in the rest of the app. I am surprised a setting in a view can propogate to the rest of the app and overwrite all of the other views in the process. Perhaps apple set the first view to determine the colors for system pop ups etc.

TableView pushed down after view change

I'm kind of new with ios development with swift 4, so I'm testing some functionalities.
I have a tableview inside a normal UIViewController:
The space left blank is intentionally, as I have some custom tabs.
When I show a detail from a table cell element, and then press back button, the table view gets pushed down (and up). Please see this where I show the problem.
The iphone is a SE with ios10. In simulations with iOS11, this does not happen. Developing in MacOS Sierra 10.12.6 with xCode 9.2.
Any clue on how to fix this?
EDIT:
Using the view debugger, I got this:
The selected area is a UITableWrapperView element. Behind it, is the UITableView, which preserve its constrains.
set
self.tableView.bounces = false
Or put this in viewDidAppear
let offset = CGPoint.init(x: 0, y:0)
self.tableView.setContentOffset(offset, animated: false)
It can happen because of SafeArea... Check you constraint which connects tableView top with navigation bar.
You can also try placing blank UIView between Navigation bar and tableView. Your custom tabs should be in this view. So tableView will be connected with the nearest view (which you have just added) and should not bounce.
Thats a really weird behavior. I recommend you to use the Xcode view debugger. You would be able to examine dimensions and constraints values by using size inspector.
The code where you set up your tableview, try implementing it in the method viewDidLayoutSubviews(in case you are doing it in viewDidLoad).
override func viewDidLayoutSubviews() {
//mention your tableview setup code here
}
A similar thing happened to me, and this method came quite handy. Hope this helps.
Try this for iOS 11.0
tableView.contentInsetAdjustmentBehavior = .never

How can I bring a view in front of another view, in Swift?

Below are how my views are organized in IB, top->bottom when the app is started.
The user can do something to make "Category Table View Header" temporarily expand over "Name View" - however once doing so, the .TouchDown action assigned to "Category Table View Header" no longer works wherever it overlaps with "Name View" (i.e., the user can tap the header anywhere it doesn't overlap with name view and it still works).
I know that may be confusing, so I drew out some boxes. On the left is the original, right is after user action - problem is on the right the action on the red box only works if the user taps the bottom half, not the top half.
My guess is its because the header is lower in the view hierarchy than the name view, but it would be hard for me to change that without messing around with a bunch of constraints.
I also tried setting nameView.hidden = true, but that doesn't work.
If you want to bring a subview to the front, you can use:
SWIFT 4 + UPDATE
self.view.bringSubviewToFront(yourView)
SWIFT 3 UPDATE
self.view.bringSubview(toFront: yourView)
Send view to back:-
SWIFT 4+ UPDATE
self.view.sendSubviewToBack(yourView)
SWIFT 3 UPDATE
self.view.sendSubview(toBack: yourView)
SWIFT 4+ UPDATE - INSERT VIEW ON SPECIFIC LOCATION IN THE STACK
parentView.insertSubview(yourView, belowSubview: requiredViewOnStack)
parentView.insertSubview(yourView, aboveSubview: requiredViewOnStack)
Swift 5.1+
UIKit draws views back to front, which means that views higher up the stack are drawn on top of those lower down. If you want to bring a subview to the front, there's a method just for you: bringSubviewToFront(). Here's an example:
parentView.bringSubviewToFront(childView)
This method can also be used to bring any subview to the front, even if you're not sure where it is:
childView.superview?.bringSubviewToFront(childView)
In Swift 5
To bring a subview to front
self.view.bringSubviewToFront(yourView)
To send a subview to back
self.view.sendSubviewToBack(yourView)
You can take a control over the order of subviews using methods: bringSubviewToFront and sendSubviewToBack from the superview.
You can access all the subviews contained by superview using self.view.subview array.
The method has been updated since swift 3
What has worked for me and hopefully for you is using :
YourView.bringSubview(toFront: yourelementA)
YourView.bringSubview(toFront: yourelementB)
Alternatively you could use the following to send the object that is in the front:
YourView.sendSubview(toBack: yourelementC)
I hope this helps
Swift 5.4
views in UIKit are drawn from the back towards the front (like adding icing onto a cake). This means that the new views will be placed on top/over the previously placed views. (e.g. Icing is the new view and the base of the cake being the old view.
You can use the following to manipulate the views Z-position using,
Forward
parent.bringSubviewToFront(child)
Backward
parent.sendSubviewToBack(child)
bringSubViewToFront did not work in my case, Take a look
As I wanted this notification count comes before image, bringSubViewToFront did not work, So I just drag this label below to stack view of my imgae and bottom label, it worked for me.
I hope this will work for all

Scroll WKInterfaceController back to top

I'm currently displaying a long label of text plus a button below it to go to the 'next' message.
When the next button is pressed the label adjusts it's height automagically thanks to watch kit's strange auto sizing mechanisms; however the screen does not change positions. I want to scroll the InterfaceController back to the top whenever the label updates. Is this possible with the current Watch Kit SDK?
This is now possible in watchOS 4. Just use the scrollToObject:atScrollPosition:animated: method.
An Apple employee said
Scrolling to the top is only supported on WKInterfaceTable. If you'd
like this functionality on a WKInterfaceController, please file a
request for it at https://bugreport.apple.com. Thanks! :)
on the developer forums https://devforums.apple.com/message/1074525#1074525
Currently, there is no API for the interface controller to scroll to top. But I did a workaround for the same.
Add a WKInterfaceTable to the top.
Set the Height of the Row Controllers Group to 2 and set the
background color to black.
Set the number of rows to 1.
Call the below method.
func scrollInterfaceToTop() {
tableView.setHidden(false)
tableView.scrollToRow(at: 0)
tableView.setHidden(true)
}
Swift 5.0 ... this works for me:
where this call is
inside the WKInterfaceController class of the WatchKit Extension app,
my 'btnPrevTee' is a button located at the top of the watch face
scroll(to:btnPrevTee, at:.top, animated:true)
ref: https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/2874208-scroll

Resources