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
Related
I want to create a blur table view cell with a top view like in the first image here:
On iPhone 8, everything works correctly but on iPhone X after viewDidLoad it's not displaying correctly:
After changing the ViewController and switching back it's working fine:
My hierarchy is as follows:
It's created in Swift 4.
Does anyone have experience with that problem? Thanks in advance!
I had the same problem in my app. What actually happens is the Xcode picks up the default storyboard of your Main.storyboard for the build. This error occurs on all the devices which have the width greater than 375. Try testing your build with iPhone X as storyboard default or call layoutSubViews() at the end of viewDidLoad.
I have an extremely bizarre problem. I created a UIButton and put it on my ViewController storyboard. I added a segue from the Button to another ViewController by pressing control and dragging. I also added an outlet to the corresponding ViewController file and added a border color and width (to the button). The segue and the button border work fine on the simulator but on a device the button border does not show up and the segue does not work.
The only thing I can think of that could be causing the problem is that a little while ago I accidentally deleted my Main.storyboard, so I dragged it from my trash back to my Xcode project. But I am pretty sure my app has worked on the device even after that incident. I wasn't sure if my viewDidLoad method was being fired (since that is where I set my border color and width for the button) so I added a print statement and found out that it was indeed being called. Take a look at the code below...
override func viewDidLoad() {
super.viewDidLoad()
print("Begin Setup Process...")
// Do any additional setup after loading the view.
calibrateButton.layer.borderColor = UIColor.white.cgColor
calibrateButton.layer.borderWidth = 3
}
How would I get this to work on my device as well? What I am doing wrong
Assign Your Border Styling Here...
Try adding the code to change layer properties of calibrateButton in viewDidLayoutSubviews(), i.e.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
calibrateButton.layer.borderColor = UIColor.white.cgColor
calibrateButton.layer.borderWidth = 3
}
Also, since you're giving borderColor as white, that might not be visible if the calibrateButton is placed on a white background. Try changing it to something else that is more visible, red for example.
First try in viewDidAppear(), if it works then cut and paste it in viewDidLayoutSubviews().
I finally solved the issue. The problem was that I didn't have a height constraint on my button. I am still not exactly sure why or how this issue occurred. Why couldn I see the button text but not the border? If the segue was not working there must have been a view on top of the button that was preventing it from going off. But anyway adding a height constraint solved the problem. In answering my own question I realize that the title of my question is misleading in relation to the solution of the problem. This problem, in the end had nothing to do with wether the app was running on a simulator or the device. I just assumed it was a device issue since it was working fine on my iPhone XR simulator but not my actual iPhone 6s. However the actual problem was the screen size and how my constraints adapted to it.
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
Xcode is not showing all the buttons, textfields, etc. I have in my storyboard. As you can see in the image below, somehow all my elements are appearing with a "watermark" in the storyboard element inspector on the left. Why is it doing this? How can I fix it? It is driving me crazy because I cannot see where my elements are when I try and add some more...
How can I solve this and why is Xcode so buggy anyways?
You have a size class enabled (note the blue bar across the center-bottomish). My guess is that all those views are missing in that size class. Click the bar, turn it back to Any x Any.
I had a similar problem, some of my IBOutlets (labels) were showing on the Storyboard, I had no AutoLayout warnings/errors and everything seemed fine but yet some of the outlets were not showing on the Simulator.
Things got even more complicated when I noticed that SOMETIMES the outlets DID show.. strange..
I ended up wrapping the call to show the view controller in a GCD method like this (swift code):
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
let vc = self.pages[0]
self.setViewControllers([vc], direction: .Forward, animated: false, completion: nil)
}
And that solved it..
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