I want to have different height for each cell in my UICollectionView. Therefore I am overriding the GetSizeForItem method in the UICollectionViewDelegateFlowLayout like this:
public class MyCollectionViewDelegateFlowLayout : UICollectionViewDelegateFlowLayout
{
public override CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
// TODO custom height logic
return new SizeF(100, 100);
}
}
This is how I create the UICollectionView and use the custom delegate class:
var flowLayout = new UICollectionViewFlowLayout()
{
ItemSize = new SizeF(320, 80),
ScrollDirection = UICollectionViewScrollDirection.Vertical,
MinimumLineSpacing = 0,
MinimumInteritemSpacing = 0,
SectionInset = new UIEdgeInsets(UIScreen.MainScreen.ApplicationFrame.Height - 100, 0, 0, 0)
};
var collectionView = new UICollectionView(UIScreen.MainScreen.ApplicationFrame, flowLayout);
collectionView.Delegate = new MyCollectionViewDelegateFlowLayout();
collectionView.RegisterNibForCell(MyCollectionCell.Nib, MyCollectionCell.Key);
var source = new MvxCollectionViewSource(collectionView, MyCollectionCell.Key);
collectionView.Source = source;
The problem is that the GetSizeForItem method never gets triggered. Any idea how to solve this?
From Xamarin's forums here, it looks like you can't use Delegate and Source at the same time.
Related
I'm developing in Xamarin and I don't know how change the height of navigation bar.
For android is easier because you've got a clear property "ActionBarSize"
I've tried a lot of things. Create a comun NavigationPage with HeightRequest property does not work
Thanks a lot
In iOS we could set a custom TitlView by using custom renderer .
using Foundation;
using UIKit;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using xxx.iOS;
using CoreGraphics;
using xxx;
using ObjCRuntime;
[assembly: ExportRenderer(typeof(ContentPage), typeof(MyPageRenderer))]
namespace xxx.iOS
{
public class MyPageRenderer: PageRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var page = Element as ContentPage;
NavigationController.NavigationBar.Hidden = true;
double height = IsiphoneX();
UIView backView = new UIView()
{
BackgroundColor = UIColor.White,
Frame = new CGRect(0,20,UIScreen.MainScreen.Bounds.Width, height),
};
UIButton backBtn = new UIButton() {
Frame = new CGRect(20, height-44, 40, 44),
Font = UIFont.SystemFontOfSize(18),
} ;
backBtn.SetTitle("Back", UIControlState.Normal);
backBtn.SetTitleColor(UIColor.Blue, UIControlState.Normal);
backBtn.AddTarget(this,new Selector("GoBack"),UIControlEvent.TouchUpInside);
UILabel titleLabel = new UILabel() {
Frame=new CGRect(UIScreen.MainScreen.Bounds.Width/2-75, 0,150, height),
Font = UIFont.SystemFontOfSize(20),
Text = page.Title,
TextColor = UIColor.Black,
Lines = 0,
};
UILabel line = new UILabel() {
Frame = new CGRect(0, height, UIScreen.MainScreen.Bounds.Width, 0.5),
BackgroundColor = UIColor.Black,
};
backView.AddSubview(backBtn);
backView.AddSubview(titleLabel);
backView.AddSubview(line);
View.AddSubview(backView);
}
double IsiphoneX()
{
double height = 44; //set height as you want here !!!
if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
{
if (UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom > 0.0)
{
height = 64; //set height as you want here !!!
}
}
return height;
}
[Export("GoBack")]
void GoBack()
{
NavigationController.PopViewController(true);
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
NavigationController.NavigationBar.Hidden = false;
}
}
}
I have UIViewController (OverlayViewController), which overlay another UIViewController(RootViewController).
In OverlayViewController I add UIView for all frame size:
var overlayView = new UIView(OverlayViewController.View.Frame);
overlayView.Alpha = 0.5f;
overlayView.BackgroundColor = UIColor.Blue;
overlayView.UserInteractionEnabled = false;
View.AddSubview(overlayView);
Then I make a hole:
var path = new CGPath();
var radius = 50.0f;
var xOffset = 100;
var yOffset = 300;
path.AddArc(overlayView.Frame.Width - radius / 2 - xOffset, yOffset, radius, 0.0f, (nfloat) (2 * 3.14), false);
var cgRect = new CGRect(0, 0, overlayView.Frame.Width, overlayView.Frame.Height);
path.AddRect(cgRect);
maskLayer.BackgroundColor = UIColor.Black.CGColor;
maskLayer.Path = path;
maskLayer.FillRule = CAShapeLayer.FillRuleEvenOdd;
overlayView.Layer.Mask = maskLayer;
overlayView.ClipsToBounds = true;
When I set overlayView.UserInteractionEnabled = false; I could touch through OverlayViewController and all clicks worked.
How I can properly use UITapGestureRecognizer so that clicks by items on the RootViewController work only inside the circle? And all the other clicks were blocked.
I tried set overlayView.UserInteractionEnabled = true;, but it doesn't help me:
tap.ShouldReceiveTouch += (recognizer, touch) =>
{
return !path.ContainsPoint(touch.LocationInView(overlayView), true);
};
View.AddGestureRecognizer(tap);
I use Xamarin, but I can understand the decision on Swift too.
I found the solution...
I make custom UIView which can set CGPath.
public class OverlayView : UIView
{
public OverlayView(CGRect frame): base(frame)
{}
private CGPath _path;
public override bool PointInside(CGPoint point, UIEvent uievent)
{
return _path.ContainsPoint(point, true);
}
public void SetPoint(CGPath path)
{
_path = path;
}
}
And set overlayView.SetPoint(path);.
In OverlayView I override PointInside and check my touch points. Also, I set overlayView.UserInteractionEnabled = true;.
I am working on Xamarin.iOS, I want to display UIPopViewController in iphone and iPad in center Screen. The below way I try the code :
public override void ViewDidLoad()
{
base.ViewDidLoad();
showButton.TouchUpInside += (sender, e) => {
// Create a UIImage view to show in the popover
UIImageView monkeyIcon = new UIImageView(new CGRect(20, 20, 200, 200));
monkeyIcon.Image = UIImage.FromFile("Abc.png");
monkeyIcon.UserInteractionEnabled = true;
// Create a view controller to act as the popover
UIViewController popover = new UIViewController();
popover.View = monkeyIcon;
popover.ModalPresentationStyle = UIModalPresentationStyle.Popover;
// Grab Image
var image = UIImage.FromFile("298-circlex.png");
// Add a close button
var closeButton = new UIButton(new CGRect(0, 20, 200, 200));
closeButton.UserInteractionEnabled = true;
closeButton.SetTitle("Close", UIControlState.Normal);
monkeyIcon.AddSubview(closeButton);
// Wireup the close button
closeButton.TouchUpInside += (button, e2) => {
popover.DismissViewController(true, null);
};
// Present the popover
PresentViewController(popover, true, null);
// Configure the popover for the iPad, the popover displays as a modal view on the
//iPhone
UIPopoverPresentationController presentationPopover = popover.PopoverPresentationController;
if (presentationPopover != null)
{
presentationPopover.SourceView = this.View;
presentationPopover.PermittedArrowDirections = 0;
presentationPopover.SourceRect = showButton.Frame;
presentationPopover.Delegate = this;
}
};
}
[Export("adaptivePresentationStyleForPresentationController:")]
public UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController)
{
return UIModalPresentationStyle.None;
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
I tried all the answer in SO and google but nothing is help to me.
Problem 1 :
- In Iphone the PopViewController is displaying in the full Screen, but I want to center and small PopView.
Problem 2 :
- In Ipad the PopViewController is displaying in the LeftSide I want to display it Center.
Any Help will be Appreciated.
If you don't want this PopoverPresentationController to be shown full screen, you should set a size to its owner Controller: popover.PreferredContentSize = new CGSize(200, 200);. Also you should set the PopoverPresentationController's configuration firstly before you present the popover.
In Ipad the PopViewController is displaying in the LeftSide I want to
display it Center.
SourceRect means the rectangle in the specified view in which to anchor the popover. You want to show this pop in the center, set this to your View.Frame. Here is my code for you referring to:
UIPopoverPresentationController presentationPopover = popover.PopoverPresentationController;
if (presentationPopover != null)
{
presentationPopover.SourceView = this.View;
presentationPopover.PermittedArrowDirections = 0;
presentationPopover.SourceRect = View.Frame;
//Configure this Delegate first before presenting.
presentationPopover.Delegate = this;
}
PresentViewController(popover, true, null);
I am using this code to create a UITextView that's added to a footer.
public override UIView GetViewForFooter(UITableView tableView, nint section)
{
var txtView = new UITextView
{
Editable = false,
Text = "Select or deselect cards from the list above and they will added or removed from the card deck,",
TextAlignment = UITextAlignment.Justified,
TextContainerInset = new UIEdgeInsets(top: 10, left: 15, bottom: 5, right: 5),
BackgroundColor = Color.Transparent.ToUIColor()
};
txtView.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
return txtView;
}
Can anyone tell me how I can make this UITextView had a height of 100?
public override UIView GetViewForFooter(UITableView tableView, nint section)
{
var txtView = new UITextView(new Rectangle(0,0,0,100))
{
..........
};
........
}
Try this hope it helps.
Override the method GetHeightForFooter
public override nfloat GetHeightForFooter(UITableView tableView, nint section)
{
return 100;
}
set frame on UITextView
public override UIView GetViewForFooter(UITableView tableView, nint section)
{
var txtView = new UITextView
{
Frame = new CGRect(0,0, tableView.Frame.Width,100),
Editable = false,
Text = "Select or deselect cards from the list above and they will added or removed from the card deck,",
TextAlignment = UITextAlignment.Justified,
TextContainerInset = new UIEdgeInsets(top: 10, left: 15, bottom: 5, right: 5),
BackgroundColor = Color.Transparent.ToUIColor()
};
txtView.TextContainer.LineBreakMode = UILineBreakMode.WordWrap;
return txtView;
}
I work on a xamarin.iOS project and I need to convert a Xamarin.Forms.View to an UIView. I managed to do what I want on Android, but not on iOS.
I have tried to use this method found here:
public static UIView ConvertFormsToNative(Xamarin.Forms.View view, CGRect size)
{
var renderer = RendererFactory.GetRenderer (view);
renderer.NativeView.Frame = size;
renderer.NativeView.AutoresizingMask = UIViewAutoresizing.All;
renderer.NativeView.ContentMode = UIViewContentMode.ScaleToFill;
renderer.Element.Layout (size.ToRectangle());
var nativeView = renderer.NativeView;
nativeView.SetNeedsLayout ();
return nativeView;
}
But it's not work in my project ...
Thank you in advance for your help !
Don't forget using Xamarin.Forms.Forms.Init(); in the appdelegate.
I tried with a Xamarin map control but it does not work. I use this and voila!
public partial class ViewControllerCliente : UIViewController........
var map = new Map(
MapSpan.FromCenterAndRadius(
new Position(37, -122), Distance.FromMiles(0.3)))
{
IsShowingUser = true,
HeightRequest = 200,
WidthRequest = 200,
VerticalOptions = LayoutOptions.FillAndExpand
};
var stack = new StackLayout { Spacing = 0 };
stack.Children.Add(map);
this.Add(ConvertFormsToNative(stack,new CGRect(0, 180,
UIScreen.MainScreen.Bounds.Width,360)));