iOS 10 ScrollView Not Working - ios

New iOS developer, have been following an iOS 9 course on Pluralsight and very simply need to be able to put some Labels in a ScrollView. All I did was select all of the labels and a button and clicked
Editor -> Embed In -> Scroll View
Here's what it looks like:
Which worked in the iOS 9 demo. I'm wondering if there is something different about iOS 10 that is not working. When I deploy to the app the labels show but you can't scroll. And yes I know how to scroll in the simulator.
I do see some warnings (which were present in the demo as well). I resolved a couple of them but still didn't work:

You can use "Reset to suggested constraint" option.Which is situated at right bottom corner in the storyboard.
You are not able to scroll because of the constraint so just override the viewDidLayoutSubviews() method into the respective viewController and provide the contentSize to the scrollView
override func viewDidLayoutSubviews() {
scrollView.contentSize = CGSize(width: self.view.frame.width, height: 1600.0)
}

Related

How to manage constraints of movable views in iOS?

I've a set of buttons which I animate in expand-collapse fashion, more like a drawer open and close action. I simply vary the X positions of these buttons to achieve this and it works fine.
While designing this layout in interface builder, I've set the constraints for all these buttons in their 'drawer-closed' state.
Problem arises when the drawer is left open and some other view on the screen gets updated.
I understand that viewDidLayoutSubviews() gets called at this instance.
Because of this, the drawer menu which was left open closes abruptly messing up with the 'drawer close' animation and the buttons don't get the expected view as in their 'drawer close' state.
I suppose the constraints of the buttons would be getting re-initialized when the viewDidLayoutSubviews() got called.
Now coming to my question - how do we set/manage constraints for views
which are going to move runtime because of animations?
I'm a beginner in iOS app development and I did go through questions asked on managing constraints programmatically or viewDidLayoutSubviews etc. but I couldn't find a standard way to follow in the situation I've encountered.
Instead of changing the frame, you could animate the change of constraints.
Short preview of how to do that:
yourConstraint.constant = newValue
UIView.animate(withDuration: animationDuration) {
yourView.layoutIfNeeded()
}
More information about this can be found here.

Xcode 9 & 9.1 selected device on storyboard affect view width on deployment device

I noticed something very strange with Xcode 9. When I'm on my main storyboard and I change the device selected to see the view controllers in the appropriate format, I've noticed that the width of my tableview on the viewDidLoad is wrong if the selected device on the storyboard is different that the device I deploy on. I have done tests and it seems that layout of the selected device on the storyboard is taken.
Does anyone has already experienced this issue?
This issue is also present on the beta version 9.1 of Xcode
I noticed the same problem when trying to apply a mask to my view's layer in order to round corners within a UITableViewCell. This answer did the trick for me.
In layoutSubviews(), call layoutIfNeeded() on any views where you need the true size at runtime. For my usage, it looks like this:
override func layoutSubviews() {
super.layoutSubviews()
print("cardView frame: \(cardView.frame)") // Storyboard size
cardView.layoutIfNeeded()
print("cardView frame: \(cardView.frame)") // Runtime device size
// Perform your layout here.
}
I hope this helps!

How to set static grouped TableView cell's width

I am relatively new to Xcode and Swift development and I have a question regarding a UITableViewController with static, grouped cells (on iPad). I want a natural solution to make the cell width not as big as the whole screen.
I have tried this:
override func viewDidAppear(_ animated: Bool) {
tableView.frame = CGRect(x: 175, y: 70, width: view.bounds.width-350, height: view.bounds.height)
}
Problem is that this looks really nasty and gets tricky with navigationbars and tabbars at the top and bottom, but especailly doesn't work properly because it is set at viewDidAppear, so when using the swipe back gesture (like in Settings), the view still appears at full width for a second.
Do you know some other way to solve this issue? Maybe some autolayout stuff, i don't know, I've tried everything I know...
Any help will be appreciated! Thank you!
As you say, autolayout is probably the way to go. If you are using Interface Builder, then you can just set your constraints there. Plenty of information out there to help with that. Here's a good place to start: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html
You should switch to using a regular UIViewController and manually add a UITableView as a subview of the root view. You'll also have to manually set the view controller as the delegate and dataSource of the table view. Then you'll be able to position the table view wherever you like. Unfortunately, you won't be able have static cells - but setting your data dynamically tends to be a better choice in the long run anyway.
The other way to achieve what you want and still use a UITableViewController is by setting it as a child view controller, but this is more fiddly and tends to be more restrictive in my experience.
If you want to manually set frames as in your code example, then you should do that inside the viewDidLayoutSubviews method.

How to resize a UIScrollView to fit its contents? ios xcode

In my application( for iphone and ipad),I have
1.MPmoviePLayer
2.tableView
3.TextView
4.CollectionView,all in a single view
The number of entries in the tableView and CollectionView may vary.The textView may also have variable content depending upon the main data.
All the four views are added as subviews for a scrollView.I want the scrollView to automatically resize its height depending on the content.I want the app run in iOS versions from iOS 7.Is there a way to do that?Using autolayout may work fine in iOS 8.But i think it will not work in iOS 7.Any suggestion is accepted.
Thanks in advance.
set below simple things in your appropriate method.
int height = CGRectGetHeight(tableview.frame)+CGRectGetHeight(CollectionView.frame)
+txtview.contentSize.height;
scrolview.contentsize = CGSizeMake(CGRectGetWidth(self.view.frame), height);
try this in your scrollViewDidScroll method...may be its work...
self.scrollview.contentSize = CGSizeMake(self.scrollview.contentSize.width,self.scrollview.contentSize.height);

iOS Today Extension Mystery Height

I'm currently working on an iOS Today Extension and I'm having an issue with the extensions height. I've tried from scratch multiple times and I can't seem to figure out where the extra height is coming from. Where am I going wrong with AutoLayout. Thanks.
Storyboard View:
Scene View:
Size Inspector for View:
Results:
I also tried using the following and it said this:
override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = CGSizeMake(0, 100);
}
Edit: Could this be an iOS bug?
I've been beating my head against the desk over this for a while now but I've noticed a repeatable pattern on the simulator as well as a physical device. If I reset contents and settings on the simulator then run the Today Extension on it, it fills the entire space (even though it's not honoring my 100 height constraints), but as soon as I 'stop' running it, the view shrinks to 100 height and there is this extra padding at the bottom. Does anyone else experience this? Is this a bug or is this something that I am causing?
First run, while 'running':
After 'stopping' and subsequent 'runs'
Finally found the issue! Apple has absurd default margins, especially on the bottom. And the only way to change them is to implement the widgetMarginInsetsForProposedMarginInsets method. Here's the swift code:
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsetsZero
}

Resources