Set ItemSize of UICollectionViewFlowLayout to width of device - xamarin

Using Xamarin, I am trying to create a UICollectionViewFlowLayout where my cells will be the width of the device and flow down vertically. Something like the Facebook news tab iOS app.
Currently to get the Cells in the layout, here is by code:
public override async void ViewWillAppear (bool animated)
{
try{
base.ViewWillAppear (animated);
var layout = new UICollectionViewFlowLayout ();
layout.MinimumInteritemSpacing = 30;
var collectionView = new NewsListView (UIScreen.MainScreen.ApplicationFrame, layout);
collectionView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
collectionView.BackgroundColor = UIColor.LightGray;
collectionView.RegisterClassForCell (typeof(NewsListViewCell), cellId);
collectionView.ReloadData ();
CollectionView = collectionView;
Logger.Log(""+CollectionView.Bounds.Width);
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
I want to use this to set the size of the item:
layout.ItemSize = new CGSize (CollectionView.Bounds.Width-20, 200.0f);
But CollectionView does not exist yet when i am creating the UICollectionViewFlowLayout,
Is there any other way i can set the size of UICollectionViewFlowLayout to the width of the device?

I found a solution to my problem. I created the layout as a global variable and then added it to the view in ViewDidLoad (), then in ViewWillAppear(..) i can set layout.ItemSize
So my code will as follows:
UICollectionViewFlowLayout layout;
public override async void ViewDidLoad ()
{
try{
base.ViewDidLoad ();
Title = "News";
layout = new UICollectionViewFlowLayout ();
layout.MinimumInteritemSpacing = 30;
var collectionView = new NewsListView (UIScreen.MainScreen.ApplicationFrame, layout);
collectionView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
collectionView.BackgroundColor = UIColor.LightGray;
collectionView.RegisterClassForCell (typeof(NewsListViewCell), cellId);
collectionView.ReloadData ();
CollectionView = collectionView;
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
public override async void ViewWillAppear (bool animated)
{
try{
base.ViewWillAppear (animated);
Logger.Log(""+CollectionView.Bounds.Width);
layout.ItemSize = new CGSize (CollectionView.Bounds.Width-20, 200.0f);
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
This way the item width of the layout is always based on device width and not hardcoded

Related

Pop the UIViewController in xamarin ios

I want to open a UIViewController from a content page and this code helps me in doing this
UIWindow Window = new UIWindow(UIScreen.MainScreen.Bounds);
var cvc = new ScanningPage();
var navController = new UINavigationController(cvc);
Window.RootViewController = navController;
Window.MakeKeyAndVisible();
but I also want a Back button on that Controller Page which navigate me back to that content Page.
this.NavigationController.PopViewController(true);
or
this.DismissViewController(true,null);
not working in this case.
I would recommend not creating a new Window and a UINavigationController...
Xamarin.Forms is contained in a single VC in the first Window, so you can obtain that view controller via:
UIApplication.SharedApplication.Windows[0].RootViewController;
So as an example Dependency service that presents and dismisses a VC (either from Forms or the view controller), you can do something like this.
Dependency interface:
public interface IDynamicVC
{
void Show();
void Dismiss();
}
iOS Dependency implementation
public class DynamicVC : IDynamicVC
{
UIViewController vc;
public void Show()
{
if (vc != null) throw new Exception("DynamicVC already showing");
vc = new UIViewController();
var button = new UIButton(new CGRect(100, 100, 200, 200));
button.SetTitle("Back", UIControlState.Normal);
button.BackgroundColor = UIColor.Red;
button.TouchUpInside += (object sender, EventArgs e) =>
{
Dismiss();
};
vc.Add(button);
var rootVC = UIApplication.SharedApplication.Windows[0].RootViewController;
rootVC.PresentViewController(vc, true, () => { });
}
public void Dismiss()
{
vc?.DismissViewController(true, () =>
{
vc.Dispose();
vc = null;
});
}
}

How to raise event when UITextField inside the UIView in Xamarin.Forms?

I have tried to raise event for UITextfield when inside the UIView.
UIView view = new UIView();
UITextField textfield = new UITextField();
textField.TouchDown += textfield_TouchDown;
view.AddSubView(textfield);
// event not raised.
void textfield_TouchDown(object s, EventArgs args)
{
// your code here
}
I have tried to add the gesture, but it is not working.
Please refer the below code
UITextField textfield = new UITextField();
UITapGestureRecognizer gestureRecognizer = new UITapGestureRecognizer();
gestureRecognizer.NumberOfTapsRequired = 1;
if (textfield != null)
{
textfield.UserInteractionEnabled = true;
gestureRecognizer.AddTarget(() =>
{
// click event for lable/image
OnTapped(gestureRecognizer);
});
textfield.AddGestureRecognizer(gestureRecognizer);
}
private void OnTapped(UITapGestureRecognizer gestureRecognizer)
{
//your code here
}
Please suggest your idea on this this?
Regards,
Srinivasan

Xamarin - adding swipe to a UIVIEW

I'm new using Xamarin and I'm trying to learn it by converting an App I built using Titanium in the past. Actually I'm stucked trying to add swipe on my UIView. I checked the documentation and also I found a similar code here in Stack Overflow, but it's not working anyway. Can someone point me what's wrong ? ty !
public partial class ViewController : UIViewController
{
UISwipeGestureRecognizer swipe;
...
}
private void addSwipe()
{
nfloat vpHeight = View.Bounds.Height;
nfloat vpWidth = View.Bounds.Width;
var mainContainer = new UIView()
{
Frame = new CoreGraphics.CGRect(0, 0, vpWidth, vpHeight),
BackgroundColor = UIColor.Red
};
swipe = new UISwipeGestureRecognizer();
mainContainer.AddGestureRecognizer(swipe);
if (swipe.Direction == UISwipeGestureRecognizerDirection.Up)
{
Console.WriteLine("up");
}
}
You need to specify a handler when you create your recognizer:
swipe = new UISwipeGestureRecognizer( (s) =>
{
if (s.Direction == UISwipeGestureRecognizerDirection.Up)
{
Console.WriteLine("up");
}
});
I found the solution on my own...
public partial class ViewController : UIViewController
{
UISwipeGestureRecognizer swipe;
...
}
private void addSwipe()
{
nfloat vpHeight = View.Bounds.Height;
nfloat vpWidth = View.Bounds.Width;
var mainContainer = new UIView()
{
Frame = new CoreGraphics.CGRect(0, 0, vpWidth, vpHeight),
BackgroundColor = UIColor.Red
};
UISwipeGestureRecognizer swipeUp = new UISwipeGestureRecognizer(OnSwipeUp);
swipeUp.Direction = UISwipeGestureRecognizerDirection.Up;
mainContainer.AddGestureRecognizer(swipeUp);
}
private void OnSwipeUp() {
Console.Writeline("Up");
}

Disabled button in UITableViewCell Xamarin.iOs

I have UITableView and each cell has button and text. Actual cell shouldn't be clickable, but button should. When I run in simulator I can hit button, but when deploy to device it is disabled. (edited)
Tried to follow many different steps to fix it from this post Button in UITableViewCell not responding under ios 7, such as ContentView.UserInteractionEnabled = False; for cell.
All of those for native iOS, maybe for Xamarin there are something else?
Any ideas what am I missing?
UITableViewCell code
this.DelayBind(() =>
{
var set = this.CreateBindingSet<CalendarCell, ActivityModel>();
set.Bind(CustomerNameLabel).To(a => a.Subject);
set.Bind(MarkAsMeetingButton).For(a => a.Hidden).WithConversion("ActivityTypeToVisibility");
set.Bind(MarkAsMeetingButton).To(a => a.SendMessageToViewModelCommand).CommandParameter(BindingContext);
set.Apply();
});
ContentView.UserInteractionEnabled = false;
UItableView code
public override void ViewDidLoad()
{
base.ViewDidLoad();
var source = new TableSource(CalendarList);
var set = this.CreateBindingSet<CalendarView, CalendarViewModel>();
set.Bind(source).To(vm => vm.CalendarList);
set.Apply();
CalendarList.Source = source;
CalendarList.ReloadData();
}
public class TableSource : MvxSimpleTableViewSource
{
private List<IGrouping<DateTime, ActivityModel>> GroupedCalendarList = new List<IGrouping<DateTime, ActivityModel>>();
public TableSource(UITableView calendarView) : base(calendarView, "CalendarCell", "CalendarCell")
{}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var currentSection = GroupedCalendarList[indexPath.Section].ToList();
var item = currentSection[indexPath.Row];
var cell = GetOrCreateCellFor(tableView, indexPath, item);
var bindable = cell as IMvxDataConsumer;
if (bindable != null)
bindable.DataContext = item;
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return (nint)GroupedCalendarList[(int)section].Count();
}
public override UIView GetViewForHeader(UITableView tableView, nint section)
{
var label = new UILabel();
var currentDate = DateTime.Now;
var titleText = GroupedCalendarList[(int)section].FirstOrDefault().ScheduledStart.Value.Date;
return label;
}
public override nint NumberOfSections(UITableView tableView)
{
return (nint)GroupedCalendarList.Count;
}
public override void ReloadTableData()
{
if (ItemsSource == null) return;
var groupedCalendarList = (ItemsSource as List<ActivityModel>).GroupBy(cl => cl.ScheduledStart.Value.Date).ToList();
GroupedCalendarList = new List<IGrouping<DateTime, ActivityModel>>(groupedCalendarList);
base.ReloadTableData();
}
}
This code works absolutely fine for simulator, but doesn't work on device, UIButton in each cell is disabled.
As #Luke said in the comments of your question said, do not use ContentView.UserInteractionEnabled = false;, since this disables any touch events on the whole view of your cell.
To achieve what you need, implement the UITableViewDelegate method ShouldHighlightRow and return false:
public override bool ShouldHighlightRow(UITableView tableView, NSIndexPath rowIndexPath) {
return false;
}
Then, the cells will not get highlighted on tap, and the RowSelected method will not get called, but your button will be clickable!

Monotouch UITableviewCells never destroyed

I have a controller which has a UITableView named WaitTableView. It has only one cell, here is the code of the UITableViewCell class :
public class TableViewWaitCell : UITableViewCell
{
public UIActivityIndicatorView activityIndicator = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray);
public UILabel lblLoading = new UILabel();
public TableViewWaitCell(UITableViewCellStyle style, string reuseIdentifier) : base (style, reuseIdentifier)
{
this.SelectionStyle = UITableViewCellSelectionStyle.None;
}
~TableViewWaitCell(){
System.Console.WriteLine("TableViewWaitCell.~TableViewWaitCell");
lblLoading = null;
activityIndicator = null;
System.GC.Collect();
}
protected override void Dispose (bool disposing){
System.Console.WriteLine("TableViewWaitCell.Dispose");
lblLoading = null;
activityIndicator = null;
base.Dispose (disposing);
GC.Collect();
}
public override void Draw (System.Drawing.RectangleF rect)
{
base.Draw (rect);
var context = UIGraphics.GetCurrentContext();
var gradient = new CGGradient(
CGColorSpace.CreateDeviceRGB(),
new float[] { 1f, 1f, 1f, 1f,
0.68f, 0.68f, 0.72f, 1f },
new float[] { 0f, 1f } );
context.DrawLinearGradient(gradient,
new PointF(rect.X+rect.Width/2, rect.Y),
new PointF(rect.X+rect.Width/2, rect.Y+rect.Height),
CGGradientDrawingOptions.DrawsAfterEndLocation);
var activityIndicatorViewFrame = new RectangleF(rect.X + rect.Width/2-10, rect.Y+10, 20, 20);
this.activityIndicator .Frame = activityIndicatorViewFrame;
this.activityIndicator.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
this.activityIndicator.StartAnimating();
this.AddSubview(this.activityIndicator);
var labelFrame = new RectangleF(rect.X, rect.Y+10+activityIndicatorViewFrame.Height, rect.Width, 35);
this.lblLoading.Frame = labelFrame;
this.lblLoading.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
this.lblLoading.TextColor = UIColor.Black;
this.lblLoading.BackgroundColor = UIColor.Clear;
this.lblLoading.TextAlignment = UITextAlignment.Center;
this.lblLoading.Text = Dictionary.GetValue("Loading");
this.AddSubview(this.lblLoading);
}
}
here is the ViewWillDisappear method of the main UIViewController :
public override void ViewWillDisappear (bool animated)
{
Console.WriteLine("SpotlightView.ViewWillDisappear");
if(this.PopoverController!=null)
this.PopoverController.Dismiss(true);
this.PopoverController = null;
this.tableView.RemoveFromSuperview();
this.WaitTableView.RemoveFromSuperview();
this.searchBar.RemoveFromSuperview();
this.tableView.Source = null;
this.tableView.Dispose();
this.tableView = null;
this.WaitTableView.Source = null;
this.WaitTableView.Dispose();
this.WaitTableView = null;
this.searchBar.Delegate = null;
this.searchBar.Dispose();
this.searchBar = null;
base.ViewWillDisappear (animated);
}
My problem is that neither the destructor nor the Dispose of my cells got called. When I run heapshot the number of instances of the TableViewWaitCell class grow up has I navigate through my app. I don't understand how the cells life-cycle is managed in Monotouch, what could have I done wrong ?
I do not see anything that would cause this problem in the code you shared. However you do not show how the cell is constructed and stored. Your objects may be kept alive by a root you are not showing in your sample code. I have created a sample below that shows Dispose and the finalizer being called.
Using the simulator the table view and the cell are collected quickly. Usually after a press the of the 'Show Table' button the second time.
Running on the device shows a different behavior. The table and cell are not collected right away. The simplest explanation is that the GC is 'tuned' to only run when it needs to. So if your app is not using much memory, all the objects will continue to live.
There are two things you can do to force the GC to run as shown in the sample.
showTable.TouchUpInside += delegate {
navController.PushViewController (new MyViewController (), true);
// Not a great idea to call Collect but you could to force it.
// GC.Collect ();
};
allocate.TouchUpInside += delegate {
// Trigger the GC by creating a bunch of objects
System.Collections.Generic.List<object> list = new System.Collections.Generic.List <object> ();
for (int i=0; i<2048; i++)
{
list.Add (new object ());
}
};
First you can call GC.Collect. However I do not recommend that. The GC will run best when you let it run when it wants to. (In most cases.) When is it acceptable to call GC.Collect?
Second just keep writing code and let the GC decide what is best. In the sample I added another button that allocates a bunch of objects and adds them to a list. So if you toggle between the table view and the main view a few times then press the allocate button a couple of times you should see the finalizers run.
using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;
namespace delete20130320
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
var mainView = new UIViewController ();
var showTable = UIButton.FromType (UIButtonType.RoundedRect);
showTable.Frame = new System.Drawing.RectangleF (10, 10, 150, 35);
showTable.SetTitle ("Show Table", UIControlState.Normal);
var allocate = UIButton.FromType (UIButtonType.RoundedRect);
allocate.Frame = new System.Drawing.RectangleF (10, 55, 150, 35);
allocate.SetTitle ("Allocate", UIControlState.Normal);
mainView.View.BackgroundColor = UIColor.White;
mainView.View.Add (showTable);
mainView.View.Add (allocate);
var navController = new UINavigationController (mainView);
showTable.TouchUpInside += delegate {
navController.PushViewController (new MyViewController (), true);
// Not a great idea to call Collect but you could to force it.
// GC.Collect ();
};
allocate.TouchUpInside += delegate {
// Trigger the GC by creating a bunch of objects
System.Collections.Generic.List<object> list = new System.Collections.Generic.List <object> ();
for (int i=0; i<2048; i++)
{
list.Add (new object ());
}
};
window.RootViewController = navController;
window.MakeKeyAndVisible ();
return true;
}
}
public class MyViewController : UIViewController
{
UITableView _tableView;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
_tableView = new UITableView (this.View.Bounds);
View.Add (_tableView);
_tableView.DataSource = new MyDataSource ();
}
~MyViewController ()
{
// Bad practice to call other managed objects in finalizer
// But for sample purposes it will be ok
Console.WriteLine ("~MyViewController");
}
protected override void Dispose (bool disposing)
{
// Bad practice to call other managed objects in Dispose
// But for sample purposes it will be ok
Console.WriteLine ("MyViewController.Dispose");
base.Dispose (disposing);
}
class MyDataSource : UITableViewDataSource
{
public override int RowsInSection (UITableView tableView, int section)
{
return 1;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell ("SomeUniqueString");
if (cell != null)
return cell;
return new TableViewWaitCell (UITableViewCellStyle.Default, "SomeUniqueString");
}
}
}
public class TableViewWaitCell : UITableViewCell
{
public TableViewWaitCell(UITableViewCellStyle style, string reuseIdentifier) : base (style, reuseIdentifier)
{
this.SelectionStyle = UITableViewCellSelectionStyle.None;
this.TextLabel.Text = "Something";
}
~TableViewWaitCell()
{
// Bad practice to call other managed objects in finalizer
// But for sample purposes it will be ok
System.Console.WriteLine("TableViewWaitCell.~TableViewWaitCell");
// Avoid forcing the GC
//System.GC.Collect();
}
protected override void Dispose (bool disposing)
{
// Bad practice to call other managed objects in Dispose
// But for sample purposes it will be ok
System.Console.WriteLine("TableViewWaitCell.Dispose");
base.Dispose (disposing);
//GC.Collect();
}
}
}
The problem came from an EventHandler which referenced a method of my view controller thus prenventing the collection of my cells and my controller.

Resources