Disable ScrollView bounce NativeScript - nativescript

Looking for a way to disable the ScrollView bounce or overflow that happens when scrolling reaches the top or bottom of the scroll view.
here how to set the settings in android.
Android scrollview remove blue light

Here is a code snippet that might do the trick for you:
if (this.content.ios instanceof UIScrollView) {
this.content.ios.alwaysBounceVertical = false;
}
Of course you need to get an instance of the <ScrollView> component from NativeScript and then the native iOS instance.

I have a small utility library that has this function and some other handy functions baked into it.
https://github.com/TheOriginalJosh/nativescript-swiss-army-knife
import {SwissArmyKnife} from 'nativescript-swiss-army-knife/nativescript-swiss-army-knife';
...
let scrollView = page.getViewById('myScrollView');
SwissArmyKnife.disableScrollBounce(scrollView);

Here is how to do it on iOS and Android.
let scrollView = page.getViewById('myScrollView');
if (app.android) {
scrollView.android.setOverScrollMode(2);
}
else if (app.ios) {
scrollView.ios.bounces = false;
}

Related

Nativescript swipe gesture not working on Android with ScrollView

I am using a swipe gesture to turn pages. It works great on ios. However, the Android version isn't working where there is a scrollview in play.
I am using Nativescript Pro UI to have a side-drawer. I want to put the gesture on the
Inside of the tkMainContent there is a scrollview.
Swipe doesn't even log a console.log event when I swipe inside the scrollview area. Is there a way to get it to work for Android?
Here is my typescript code:
let cbView = this.contentbody.nativeElement;
cbView.on('swipe', (args: SwipeGestureEventData) =>{
console.log("Swipe Direction: " + args.direction);
let topNextView = cbView.getViewById('topNext');
let topBackView = cbView.getViewById('topBack');
this.playerService.setIsBusyLoading(false);
if(args.direction === 1) {
topBackView.notify({eventName: 'tap', object: topBackView});
}
if(args.direction === 2) {
topNextView.notify({eventName: 'tap', object: topNextView});
}
});
}
As I said I"m not getting Swipe Direction 2 when swiping on Android. If I swipe above where I have the scrollview start, such as in the heading of my document, then it picks up the swipe.

How to disable scroll in Webview?

Is there a way i can disable scroll in webview?
For instance
<WebView rc="https://www.npmjs.com/package/nativescript-
openurl" height="300" tap="ontapvw" ></WebView>
Please find the native script playground link attached:
https://play.nativescript.org/?template=play-js&id=hh7ITB&v=2
You can disable the scroll by accessnig the native WebView and apoplying a native solution (direct access to the native APIs is one of the greatest advantages of nativeScript as this allows you to work with native solutions)
Example for Android
XML
<WebView loaded="onWebViewLoaded" src="https://www.npmjs.com/package/nativescript-openurl" height="300" ></WebView>
JavaScript
function onWebViewLoaded(args) {
let wv = args.object;
console.log(wv);
if(isAndroid) {
let webViewAndroid = wv.nativeView;
console.log(webViewAndroid);
// Hide the scrollbars, but not disable scrolling:
webViewAndroid.setVerticalScrollBarEnabled(false);
webViewAndroid.setHorizontalScrollBarEnabled(false);
// Disable scrolling
let myListener = new android.view.View.OnTouchListener({
onTouch: function (view, event) {
return (event.getAction() == android.view.MotionEvent.ACTION_MOVE);
}
})
webViewAndroid.setOnTouchListener(myListener);
}
}
exports.onWebViewLoaded = onWebViewLoaded;
Playground demo can be found here

Xamarin ios map circle overlay click

I'm following the maps example https://developer.xamarin.com/samples/xamarin-forms/customrenderers/map/circle/ to create custom renderer to overlay circles. It seems to be working fine for all 3 platforms except I am unable to find any click event for iOS.
I am building a xamarin forms app and able to find out the click event on both Android and UWP but iOS seems to be far fetched.
Is there any way to do it?
iOS doesn't provide the API which detects the click event.
As a alternative workaround, we can add UITapGestureRecognizer on the mapview and judge if the tap point locates inside the circle.
Solution
//xxx
nativeMap.AddOverlay(circleOverlay);
nativeMap.AddGestureRecognizer(new UITapGestureRecognizer(TapHandle));
void TapHandle(UITapGestureRecognizer tap)
{
MKMapView mapView = tap.View as MKMapView;
CGPoint tapPoint = tap.LocationInView(mapView);
CLLocationCoordinate2D tapCoordinate = mapView.ConvertPoint(tapPoint, tap.View);
MKMapPoint point = MKMapPoint.FromCoordinate(tapCoordinate);
foreach(IMKOverlay overlay in mapView.Overlays)
{
MKCircleRenderer render = GetOverlayRenderer(mapView, overlay) as MKCircleRenderer;
CGPoint datPoint = render.PointForMapPoint(point);
render.InvalidatePath();
if (render.Path.ContainsPoint(datPoint,false))
{
break;
}
}
}

In NativeScript on Android, how do I prevent a SearchBar from gaining focus on page load?

I have a SeachBar inside a ScrollView. In iOS all is good. On Android the ScrollView automatically scrolls to the SearchBar, adds focus to it and displays the soft keyboard. I can hide the softkeyboard by adding android:windowSoftInputMode="stateHidden" as an activity in the AndroidManifest.xml file but I can't work out how to prevent the focus (and hence the auto scroll). Any help would be much appreciated.
Using Angular2:
app/my.component.html
<StackLayout (loaded)="onSearchLayoutLoaded($event)">
<SearchBar hint="search here" (loaded)="onSearchBarLoaded($event)">
</SearchBar>
</StackLayout>
app/my.component.ts
onSearchLayoutLoaded(event) {
if (event.object.android) {
event.object.android.setFocusableInTouchMode(true);
}
}
onSearchBarLoaded(event) {
if (event.object.android) {
event.object.android.clearFocus();
}
}
This eliminates unnecessarily having to use template reference variables.
For Android you need to do a couple of things. If you were using a native Android layout, lets say LinerLayout you would set android:focusableInTouchMode="true" and that should work for most use cases. So with NativeScript you're going to use the associated method on the parent of your SearchBar and then call `clearFocus() on the searchbar.
Example
function removeSearchFocus() {
// get the parent of the searchbar
var parent = page.getViewById('parentLayout');
var searchBar = page.getViewById('mySearchBar');
if (parent.android) {
parent.android.setFocusableInTouchMode(true);
parent.android.setFocusable(true);
searchBar.android.clearFocus();
}
}
Then attach this function to one of the page navigation event, or dump the important pieces into a current page event you might already have in your app. Just assign the parent an ID and the SearchBar so you can get it by the ID and this should work.
How about adding a endEditing to the page loaded or loading event?
searchBar = page.getViewById('your-searchbar');
searchBar.ios.endEditing(true);
or set focus to another element, where you want the focus to be, e.g.
somethingElse = page.getViewById('your-top-bar');
somethingElse.focus();

Capture touch events in Xamarin.Forms for Android

I am creating an app that allows the user to drag and drop to put one image on top of another using Xamarin.Forms.
On iOS I managed to hack together a workable gesture recognizer renderer by creating a custom ContentView and a custom renderer for it, which then attaches native gesture recognizers to itself based on the GestureRecognizers elements. To do that I disable the default EventTracker and implement my own, with overridden GetNativeRecognizer method
in the custom renderer:
public InteractiveContentViewRenderer () : base ()
{
AutoTrack = false;
events = new InteractiveEventTracker (this);
}
in the custom event tracker:
public InteractiveEventTracker (IVisualElementRenderer renderer) : base (renderer)
{
this.Renderer = renderer;
}
protected override MonoTouch.UIKit.UIGestureRecognizer GetNativeRecognizer (Xamarin.Forms.IGestureRecognizer recognizer)
{
var gestureRecognizer = base.GetNativeRecognizer (recognizer);
if (gestureRecognizer == null) {
// here I find my own native recognizer and return it
}
}
On Android, however, so far I haven't figured out how to achieve the same thing. There's no EventTracker in Android, I think I'll have to implement some Android View for this to work but I haven't figured it out so far.
Has anyone managed to hack together touch events in Xamarin.Forms for Android? I'd like to know at least the basic structure of the hack?
Read this: http://blog.twintechs.com/cross-platform-compositional-gesture-advanced-xamarin-forms-techniques-for-flexible-and-performant-cross-platform-apps-part-4
There's a lot about custom gestures in Xamarin.Forms
The source is here: https://github.com/twintechs/TwinTechsFormsLib/tree/master/TwinTechsForms/TwinTechsForms.Droid/TwinTechs/Gestures

Resources