UiScrollview with nested image looks weird - uiscrollview

I have a UIScrollView inside a UIViewController (subclassed by ImageViewController). The ViewController itself is part of a NavigationController's stack. Now, apart from having a navigation bar, I want the ScrollView to take all of the available room on the screen. The UIImageView inside the scrollview should then fill the available room of the scroll view. You can see the current state at the bottom of this posting.
class ImageViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
var imageView: UIImageView?
var image: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
if let image = image {
imageView = UIImageView(image: image)
if let imageView = imageView {
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: image.size)
scrollView.addSubview(imageView)
scrollView.contentSize = image.size
let scaleHeight = scrollView.frame.size.height / scrollView.contentSize.height
let scaleWidth = scrollView.frame.size.width / scrollView.contentSize.width
let minimumScale:CGFloat = min(scaleHeight, scaleWidth)
let maximumScale:CGFloat = max(scaleHeight, scaleWidth)
scrollView.minimumZoomScale = minimumScale
scrollView.maximumZoomScale = maximumScale
scrollView.zoomScale = maximumScale
}
}
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
}
The code leaves me with unnecessary borders (left, right, top). How do I get rid of them?
EDIT: With #Bxtr's suggestion and another stackoverflow thread I was able to remove the borders left and right to the scroll view. After some more digging I found out that by deactivating Adjust Scroll View Insets, the image inside the scroll view can be correctly vertically positioned. Still, I do not get the reason for the vertical misplacement in the first place...

Have you checked the margin/padding values, because it kinda looks so (same size on left and right border). If it is not the case, could you please also post your xml file of the activity so we can have every part of the puzzle to help you ?

scrollView.contentSize = image.size;
you have to tweek this line. You are explicitly setting scroll view content size to the image size. You have to set content size to fit the Width of Screen.

You can use a UIView in UIScrollView, and that UIView contains UIImage.
You need to set constraints properly.

After some more digging I found out that by deactivating Adjust Scroll
View Insets, the image inside the scroll view can be correctly
vertically positioned. Still, I do not get the reason for the vertical
misplacement in the first place...
The reason is that the view controller's automaticallyAdjustsScrollViewInsets property is by default YES, the following is from apple documentation:
automaticallyAdjustsScrollViewInsets
A Boolean value that indicates
whether the view controller should automatically adjust its scroll
view insets.
Default value is YES, which allows the view controller to adjust its
scroll view insets in response to the screen areas consumed by the
status bar, navigation bar, and toolbar or tab bar. Set to NO if you
want to manage scroll view inset adjustments yourself, such as when
there is more than one scroll view in the view hierarchy.
Besides setting automaticallyAdjustsScrollViewInsets = No, you can pin the scrollView to the topLayoutGuide (instead of to the top of the viewController's view) when using autoLayout.

Related

Animating constraints with layer-backed NSView

I'm attempting to implement an animation that shows/hides a view in a horizontal arrangement. I'd like this to happen with slide, and with no opacity changes. I'm using auto-layout everywhere.
Critically, the total width of the containing view changes with the window. So, constant-based animations are not possible (or so I believe, but happy to be proved wrong).
|- viewA -|- viewB -|
My first attempt was to use NSStackView, and animate the isHidden property of an arranged subview. Despite seeming like it might do the trick, I was not able to pull off anything close to what I was after.
My second attempt was to apply two constraints, one to force viewB to be zero width, and a second to ensure the widths are equal. On animation I change the priorities of these constraints from defaultHigh <-> defaultLow.
This results in the correct layout in both cases, but the animation is not working out.
With wantsLayer = true on the containing view, no animation occurs whatsoever. The views just jump to their final states. Without wantsLayer, the views do animate. However, when collapsing, viewA does a nice slide, but viewB instantly disappears. As an experiment, I changed the zero width to a fixed 10.0, and with that, the animation works right in both directions. However, I want the view totally hidden.
So, a few questions:
Is it possible to animate layouts like this with layer-backed views?
Are there other techniques possible for achieving the same effect?
Any ideas on how to achieve these nicely with NSStackView?
class LayoutAnimationViewController: NSViewController {
let containerView: NSView
let view1: ColorView
let view2: ColorView
let widthEqualContraint: NSLayoutConstraint
let widthZeroConstraint: NSLayoutConstraint
init() {
self.containerView = NSView()
self.view1 = ColorView(color: NSColor.red)
self.view2 = ColorView(color: NSColor.blue)
self.widthEqualContraint = view2.widthAnchor.constraint(equalTo: view1.widthAnchor)
widthEqualContraint.priority = .defaultLow
self.widthZeroConstraint = view2.widthAnchor.constraint(equalToConstant: 0.0)
widthZeroConstraint.priority = .defaultHigh
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
self.view = containerView
// view.wantsLayer = true
view.addSubview(view1)
view.addSubview(view2)
view.subviewsUseAutoLayout = true
NSLayoutConstraint.activate([
view1.topAnchor.constraint(equalTo: view.topAnchor),
view1.bottomAnchor.constraint(equalTo: view.bottomAnchor),
view1.leadingAnchor.constraint(equalTo: view.leadingAnchor),
// view1.trailingAnchor.constraint(equalTo: view2.leadingAnchor),
view2.topAnchor.constraint(equalTo: view.topAnchor),
view2.bottomAnchor.constraint(equalTo: view.bottomAnchor),
view2.leadingAnchor.constraint(equalTo: view1.trailingAnchor),
view2.trailingAnchor.constraint(equalTo: view.trailingAnchor),
widthEqualContraint,
widthZeroConstraint,
])
}
func runAnimation() {
view.layoutSubtreeIfNeeded()
self.widthEqualContraint.toggleDefaultPriority()
self.widthZeroConstraint.toggleDefaultPriority()
// self.leadingConstraint.toggleDefaultPriority()
NSAnimationContext.runAnimationGroup({ (context) in
context.allowsImplicitAnimation = true
context.duration = 3.0
self.view.layoutSubtreeIfNeeded()
}) {
Swift.print("animation complete")
}
}
}
extension LayoutAnimationViewController {
#IBAction func runTest1(_ sender: Any?) {
self.runAnimation()
}
}
Also, some potentially relevant, but so far unhelpful, related questions:
Animating Auto Layout changes concurrently with NSPopover contentSize change
Animating Auto Layout constraints with NSView.layoutSubtreeIfNeeded() not working on macOS High Sierra
Hide view item of NSStackView with animation

iOS vertical center dynamic height stack layout in autolayout

I am trying to create a vertically and horizontally centered stack view with dynamic height content. Something like this:
I and I am almost there, but the problem is that when I add a button which height should be dynamic (scale with label) I get this: (Note the overlapping buttons).
Here's what I currently have:
Without that button with the long label, the view looks good. Now I've read about the concepts of how the auto layout works, that It needs to know the height of the content to center it. But about the cases when the content height is unknown?
Setting the titleLabel of a UIButton to multi-line (changing Line Break to Word Wrap) doesn't change the intrinsicContentSize of the button, so you have to do it yourself.
Here is one approach. Subclass UIButton like this:
class CheckboxButton: UIButton {
override var intrinsicContentSize: CGSize {
return titleLabel?.intrinsicContentSize ?? CGSize.zero
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel?.preferredMaxLayoutWidth = titleLabel?.frame.size.width ?? 0
super.layoutSubviews()
}
}
You can confirm the sizing is being done correctly by giving your button a background color (during development) to clearly see its bounds / frame.
After that, the spacing should be handled correctly by auto-layout.

Resize NSPopover does not update

I have an NSPopover where I change the content view of but when I set the view controller to something bigger than before, it shows the size that it was and I need to close and open it.
static func changeView(controller: NSViewController) {
let newSize = NSSize.init(width: controller.view.frame.size.width, height: controller.view.frame.size.height)
popover.contentViewController = controller
popover.contentSize = newSize
//^ this does not update or whatever
}
all code
That is the code I use to change the view but it does not update the popover.
EDIT
It only resizes when I'm at my home screen but when I open it when Xcode is open fullscreen it doesn't work. :/
So to resize a NSPopover correctly you need to set animates to false like AppDelegate..

Why my UITextView text is not show from starting and hide under Navigation Bar

I wonder why my textview text didn't show up from starting text.
This is the image from my Xcode IB
When I run it on my iPhone 6 Simulator and others
Actually it should start from "Example Notification...."
Please help me out.
Those who didn't start UITextView from start,here is the answer.I am sure it worked 100% on iOS 8 and higher on any devices.
1.You need to deselect these at your view controller where textview is applied
Go to ViewController > Attribute Inspector > Search Extend Edges > Under Top Bars & Under Button Bars
2.Add the following code at your view controller
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.yourTextView.setContentOffset(CGPoint.zero, animated: false)
}
Special Thanks to #El Captain
To maintain UINavigationBar translucent and allow user to rotate the device while reading UITextView I used something like this:
private var firstLayout = true
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if firstLayout {
firstLayout = false
textView.setContentOffset(
CGPoint(x: 0, y: -topLayoutGuide.length), animated: false)
}
}
Tested on iOS 9 and iOS 12 Beta.
In my case it was iOS 13. UITextView was constrained to edges. It's content was set on did load, after that text view automatically scrolled to the bottom causing large title to shrink.
What helped me is to wrap setText in to async:
DispatchQueue.main.async {
textView.text = logs
}
You can also keep UINavigationBar isTranslucent and UIScrollViewContentInsetAdjustmentAutomatic,
Just to fix the textView is hidden by navigationBar, then add the below code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[textView setContentOffset:CGPointMake(0, -self.view.layoutMargins.top) animated:NO];
}

Xcode: How to fix the toolbar for a tableView?

I have a tableView and want to add a toolbar at the bottom with a sync button and the latest sync date (like on the Mail-app on the iPhone). But the Scrollbar should be fixed, so that it doesn't move when I scroll trough the table view.
Right now, I'm just adding the searchBar programmatically by:
self.tableView.tableFooterView = self.toolbar;
Another problem linked here is the fact, that I'm using the MasterDetailsView Controller template of Xcode. But I can't just drag a toolbar into my MasterView (containing the tableview). My first guess is that its just fixed because of the template I used but I'm not quite sure about it.
Thanks in advance!!
Instead of using a UITableViewController try using a plain UIViewController which conforms to UITableViewDelegate and UITableViewDataSource. Add a UITableView as a subview to the view controller's main view and link its delegate and datasource to the view controller.
Now, you can add any other UIViews to the main view which will appear fixed on top of the UITableView.
In your Story board for that ViewController, drag Bar Button Item to the Bottom after the tableView and in viewDidLoad method.. do the following..
self.navigationController.toolbarHidden = NO;
I think I found a really good solution to this.
In the storyboard view, drop the toolbar out of the Tableview. In the story board view this will leave it 'floating' above the view and the tool bar will no longer appear in your compiled code.
Too fix this, add the following to your view controller code
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UIView()
let toolBarView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
mainToolBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44)
toolBarView.addSubview(mainToolBar)
v.addSubview(toolBarView)
return v
}

Resources