UIView constraint behaving differently when using iPhone X - ios

I have a viewcontroller with a UITableView that takes up the majority of the screen. At the bottom of the viewcontroller, I have a UIView called controlView with some buttons. When the viewcontroller first loads, I set the NSLayoutConstraint that manages the height of controlView to 1 so that controlView is not visible.
#IBOutlet weak var controlViewHeightConstraint: NSLayoutConstraint!
controlViewHeightConstraint.constant = 1.0
When a user taps on a tableview cell, I adjust controlViewHeightConstraint.constant to make it visible
controlViewHeightConstraint.constant = heightConstant //heightConstant = 65
self.view.layoutIfNeeded()
This has been working perfectly for the iphone 5, 6, 7, 8, and 8+. However, with the X, when I call controlViewHeightConstraint=1 in viewDidLoad, the controlView is still visible. Any idea why it is appearing when it should not be visible.
storyboard setup
iPhone 8+ before tapping cell
iPhone 8+ after tapping cell
iPhone X before tapping cell
iPhone X after tapping cell

Solved this issue by adjusting the top constraint of the controlView to be below the screen by default and adjusting the constraint to move the view up to become visible. For some reason adjusting the height constraint of the view would not work with the iPhone X. Might be due to complications with the bottom safe area portion of the iPhone X.

Related

Layout issues in UITableView after updating to Xcode 9

After I updated to Xcode 9, UITableView in iOS 10 device is behaving very strangely. The tableView cell sizes are not maintained. It it resizing by itself and not following the constraints and cell sizes I had explicitly coded. The gap between the tableView cells is black all of a sudden. However, these layout issues are not reciprocated in iOS 11 handset.
In a nutshell, the layout is completely screwed in iOS 10 devices. Is anyone else having backward compatibility after updating to Xcode 9? If so how did you resolve it.
To change the frame width of the custom table View cell cell I am using the frame property. The code I am using is
override var frame: CGRect {
//Decrease frame width of the tableViewCell
get {
return super.frame
}
set (newFrame) {
var frame = newFrame
frame.origin.x += 8
frame.size.width = frame.size.width-16
super.frame = frame
}
}
Below is how the tableViewCell is presented in iOS 11 which is correct
this is how the tableViewCell is presented in iOS 10 which is incorrect
[
As you can see the gap from right margin to the tableView Cell is more.
Throw away your existing code. Don't touch the cell's frame in any way.
To make cells that look narrower than the table, give the cell a clear background and put an opaque view inside the content view that is inset from the cell's bounds (or you could use the cell's background view for this). Use autolayout to configure this, so that it works regardless of screen size.

UITableView Scroll Indicator in Left Side

I need to bring Scroll indicator to the left side of a table view.
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, self.tableView.bounds.size.width-8);
Above code is working fine for iPhone 5, 5C etc.
But scroll indicator is not positioned well in iPhone 6. It shows some padding from left side.
In viewDidLoad method, the bounds of the view are not set correctly and if view is set in nib/storyboard, bounds are set accroding to size of view set there.
I guess, in your nib.storyboard you have chosen iPhone 5S/iPhone 5S size to design the view and henceit works in those devices.
If you set tableView's scrollIndicatorInsets in viewDidLayoutSubviews method, the indicator inset should set correctly.

Resizing a UITextView programmatically in Swift 2 to enlarge its width

EDIT: I post a screenshot of what I would like to implement in iPad and iPhone. When using the button in iPhone, the UITableView opens and has to cover a part of the UITextView.
I have a universal project for iPhone and iPad. In the main storyboard there are two controls, an UITextField and a UITableView. The UITextField keeps the left 1/3 portion of the screen and the UITableView the remaining part of the screen.
When in iPhone, I hide the UITableView and I would like that the UITextView keeps all the screen. Then, when the user taps on a button, the UITableView appears over the text field (I dismiss the UITableView as soon as the user selects a choice).
How can I have the tableView to enlarge in a way that its right border, go to the right border of the screen?
One way to implement what you described is to start with the TableView "outside" of the scene, with a negative Leading Space to the main view. Them when users press the button, the value of the Leading Space Constraint is set to zero.
Example:
Final result:
ViewController code:
class ViewController: UIViewController {
#IBOutlet weak var tableViewLeadingMargin: NSLayoutConstraint!
#IBAction func revealTableView(sender: UIButton) {
UIView.animateWithDuration(
3.0,
animations: {
self.tableViewLeadingMargin.constant = 0
self.view.layoutIfNeeded()
}
)
}
}
There's just one caveat: you'll need to adjust the width of the TableView programatically, based on the iPhone screen size.
The answer above the iPhone issue. For iPad, I'd suggest using Size Classes, creating a specific layout/constraints for them:
To illustrate:

Bottom UIToolbar off screen on iPhone 4 and 4S

My problem is very similar to UIToolbar not displaying on iPhone 4 and iPhone 4s
I am using Xcode 7 and am trying to get a 'legacy' UINavigationController based iPhone app up and running on various iPhone screen sizes. By legacy , I mean it does not use Storyboards etc. The views are loaded from an .xib.
The app is a classic UINavigationController app with UITableViewControllers but with a UIToolBar at the bottom underneath the table view. The TableView and ToolBar are subviews of the view of the ViewController.
Works great on iPhone5/5s/6/6s.
But on the iPhone 4/4s the toolbar is off the screen. Oddly if I rotate the screen to landscape, the toolbar appears. Rotate back, it vanishes. I know this seems like prehistoric iOS code, but I am completely at a loss here and have wasted hours fiddling in Xcode and IB. I know I am missing something obvious.
It seems like the height of your table view is set to be a fixed height where the toolbar is visible beneath the table view on iPhone 5 and up. To get the toolbar to display on iPhone 4/4s requires reducing the height of the table view's frame using something like the following code.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if (onIphone4 && portraitOrientation) {
CGFloat height = 480 - self.toolbar.frame.size.height;
self.tableView.frame = CGRectMake(0, 0, 320, height); // for iPhone 4/4s
} else if (onIphone5 && portraitOrientation) {
CGFloat height = 568 - self.toolbar.frame.size.height;
self.tableView.frame = CGRectMake(0, 0, 320, height); // for iPhone 5 and up
}
}
You will probably need to change the y position in the frame origin to account for the status bar and navigation bar if they are also present. In that case, the overall frame height will need to be adjusted for those changes. This is the frame-based way of adjusting view sizes.
The alternative and preferred method is to add auto layout constraints in the XIB for the toolbar and the table view in Interface Builder so that they will maintain the proper size and position relative to the screen dimensions.
I found the issue.. it was the "Full Screen at Launch" checkbox in IB, for the main "UIWindow"... and I quote from Apple documentation....
"If you choose to create a window in Interface Builder, be sure to select the Full Screen at Launch option in the Attributes inspector so that the window is sized appropriately for the current device."
Now please excuse me while I slam head against wall..way to waste a Sunday!

UIScrollView contentOffset is set automatically when switched to foreground

I am developing an iPhone application. I have my code implemented this way:
RootViewController has UIScrollView as its view. Then RootViewController pushes another UIViewController say vc which again has UIScrollView (sv) as its view.
The bounds/frame size of sv is (320, 460) and content size is (320, 520). So, sv is now scrollable vertically. Hence, I have set content offset of sv to be (0, 60).
I switch to background using Home button of iPhone and again put my application to Foreground. In this case my sv content offset is set to (0, 0) automatically with animation.
When I tried to override setContentOffset: I noticed that some library call [adjustsIfNeeded], sets content offset to be (0, 0).
Why is this happening?
This was because, my base view of UIViewController was UIScrollView. I changed it to a UIView and then added UIScrollView as its subview. Now, it works fine.

Resources