Is there any way to resize a cell in UICollectionView when a device changes from portrait to landscape orientation? With the new iOS 6 auto layout? I don't want to reload the whole UICollectionView.
I found the solution if anybody was wondering. Solution:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] == 0 && ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown )){
return CGSizeMake(768, 400);
}else if ([indexPath row] == 0 && ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)){
return CGSizeMake(1024, 400);
}
return CGSizeMake(255, 200);
}
So the method sizeForItemAtIndexPath specifies a width and height for each cell using CGSize. All you need to do is detect the orientation of the cell, and return the specific height you want. Keep in mind I was only changing one cell for design purposes.
After that, i called [invalidate layout] in :
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
which basically gets the layout of every cell again, also it doesn't reload the UICollectionView's data!
Related
I need to add some view in iOS 7. And in the same time my project have to work on iOS 6.
How can I do it programmacly? I want to see the same position of "i"-button on 2 versions of app.
Here's a category I put on a ViewController. It might not be the smartest method but it works for my views. All my apps build the UI's programatically. If you set the frame of a view to this after it has been loaded, it will behave the same in iOS 7 as in earlier versions. The code isn't kludgy in that it doesn't rely on the version rather the characteristics of the superview. I'm open to any comments.
#implementation UIViewController (effectiveContentFrame)
// determine frame size and offset to get the max frame we can get for the current view and orientation
// needed this after the introduction of iOS 7 where frames are handled slightly differently
// note: it uses the different heights/widths at the current rotation
/*
iOS 6 portrait
application Frame: = (0.000000,20.000000) 320.000000x460.000000 )
superview Frame = (0.000000,20.000000) 320.000000x460.000000 )
iOS7 portrait
application Frame: = (0.000000,20.000000) 320.000000x460.000000 )
superview Frame = (0.000000,0.000000) 320.000000x480.000000 )
*/
- (CGRect) effectiveContentFrame
{
CGRect rect;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
CGFloat statusBarHeight = 0.;
rect = self.view.superview.frame; // the frame will be relative to the superview. Note in iOS6 the root superview frame includes the
if (UIInterfaceOrientationIsPortrait(orientation))
{
rect.size.height += rect.origin.y; // total height
statusBarHeight = statusBarFrame.size.height;
}
else
{
rect.size.height += rect.origin.x; // total height
statusBarHeight = statusBarFrame.size.width;
}
CGFloat extraHeight = 0; // this is going to be how much extra height we have to take off of the total height of the frame,
// which is going to be the status bar+navbar+toolbar heights
CGFloat topHeight = 0; // this is the height of the stuff before the frame, which we are going to use as an offset
if (![UIApplication sharedApplication].statusBarHidden)
extraHeight += statusBarHeight;
if (!self.navigationController.navigationBar.isHidden)
extraHeight += self.navigationController.navigationBar.frame.size.height;
topHeight = extraHeight;
if (!self.navigationController.toolbar.isHidden)
extraHeight += self.navigationController.toolbar.frame.size.height;
rect.origin.y = topHeight - rect.origin.y; // in iOS6 the status bar and navbar is already included in the origin but not in iOS7 .
if (rect.origin.y < 0)
rect.origin.y = 0;
rect.size.height -= extraHeight; // subtract status/nav and toolbar heights from the total height left in the superview
return rect;
}
It seems that type of Top Bar of your main view is Translucent Navigation Bar. Try to change it in IB to Opaque Navigation Bar.
I have had the same problem when I upgraded to the new xCode. I think this is because in older version opaque type was first in the list and now translucent on it's place.
Also, you can try self.edgesForExtendedLayout = UIRectEdgeNone; in your UIViewController
In Xcode 5 IB you can find iOS 6 / 7 deltas. All that you need is just adjust them.
The answer in C#
public static RectangleF GetUsableContentSize(this UIViewController viewController) {
RectangleF rect;
UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
RectangleF statusBarFrame = UIApplication.SharedApplication.StatusBarFrame;
float statusBarHeight = 0.0f;
var view = viewController.View;
if (view.Superview != null)
rect = view.Superview.Frame; // the frame will be relative to the superview. Note in iOS6 the root superview frame includes the
else
rect = view.Frame;
if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown)
{
rect.Height += rect.Y; // total height
statusBarHeight = statusBarFrame.Height;
}
else
{
rect.Height += rect.X; // total height
statusBarHeight = statusBarFrame.Width;
}
float extraHeight = 0; // this is going to be how much extra height we have to take off of the total height of the frame,
// which is going to be the status bar+navbar+toolbar heights
float topHeight = 0; // this is the height of the stuff before the frame, which we are going to use as an offset
if (!UIApplication.SharedApplication.StatusBarHidden)
extraHeight += statusBarHeight;
if (!viewController.NavigationController.NavigationBar.Hidden)
extraHeight += viewController.NavigationController.NavigationBar.Frame.Height;
topHeight = extraHeight;
if (!viewController.NavigationController.Toolbar.Hidden)
extraHeight += viewController.NavigationController.Toolbar.Frame.Height;
rect.Y = topHeight - rect.Y; // in iOS6 the status bar and navbar is already included in the origin but not in iOS7 .
if (rect.Y < 0)
rect.Y = 0;
rect.Size.Height -= extraHeight; // subtract status/nav and toolbar heights from the total height left in the superview
return rect;
}
I'm make an iPhone app, in this app the user can send a SMS.
On my iPhone 4 is my screen perfect, but on my iPhone 5 i get a blank box.
iPhone only. iOS 6x
Screenhot (iphone5): http://i.stack.imgur.com/ZOqu1.png
#import "smsview.h"
#interface smsview ()
#end
#implementation smsview
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultCancelled)
NSLog(#"Message cancelled");
else if (result == MessageComposeResultSent)
NSLog(#"Message sent");
else
NSLog(#"Message failed");
}
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
-(IBAction)Text:(id)sender
{
{
[self sendSMS:#"" recipientList:[NSArray arrayWithObjects:#"+1-234-567-8910", nil]];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
Follow these steps if u failed to.
Are you using the latest version of Xcode if not download from here.
You can use the Auto layout feature and create the design the using iPhone 5 screen resolution and it will work for the both 4" and 3.5" devices, but in this case you should have a enough knowledge of layout manager.
Set a 4-inch launch image for your app. This is how you get 1136 px screen height (without it, you will get 960 px with black margins on top and bottom), To adapt your app to the new taller screen, the first thing you do is to change the launch image to: Default-568h#2x.png. Its size should be 1136x640 (HxW). Yep, having the default image in the new screen size is the key to let your app take the whole of new iPhone 5's screen..
Hopefully everything should work if you had set auto resizing masks properly.
If you didn't, adjust your view layouts with proper auto resizing masks or look into Auto Layout if you only want to support iOS 6 going forward.
If there is something you have to do for the larger screen specifically, then it looks like you have to check height of [[UIScreen mainScreen] bounds] (or applicationFrame, but then you need to consider status bar height if it's present) as there seems to be no specific API for that.
for instance:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
//iPhone 4 specific
} else {
//iPhone 5 specific
}
Hope this solves your problem.
I'm creating an iOS application which should be supported both landscape and portrait. So I have used 2 views for these 2 orientations. But now the problem is I don't know how to connect IBoutlets to the file's owner. Can't we use one IBOutlet set to both modes. I haven't done any application which supported both modes. Please any one can tell me how can I fixed this problem.
Thanks
Finally I found this according to this way. I created 2 views and defined seperate IBOutlets set. When change the orientation I assigned the values in one mode to other mode's outlet's values. This did in (void)didRotate:(NSNotification *)notification
`
-(void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation newOrientation = [[UIDevice currentDevice] orientation];
if (newOrientation != UIDeviceOrientationUnknown && newOrientation != UIDeviceOrientationFaceUp && newOrientation != UIDeviceOrientationFaceDown)
{
if (orientation != newOrientation)
{
//ORIENTATION HAS CHANGED
NSLog(#"Changed Orientation");
// Do orientation logic
if (
((newOrientation == UIDeviceOrientationLandscapeLeft || newOrientation == UIDeviceOrientationLandscapeRight)) &&
((orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown))
)
{
NSLog(#"Changed Orientation To Landscape");
self.orientationMode=#"landscape";
self.txtInsidernameLandscape.text=self.txtInsiderName.text;
self.txtCompanynameLandscape.text=self.txtCompanyName.text;
self.txtAddressLandscape.text=self.txtAddress.text;
self.txtPhoneLandscape.text=self.txtTelephone.text;
self.txtEmailLandscape.text=self.txtEmail.text;
// Clear the current view and insert the orientation specific view.
[self clearCurrentView];
[self.view insertSubview:mainLandscapeView atIndex:0];
//Copy object states between views
//SomeTextControlL.text = SomeTextControlP.text;
}
else if (
((newOrientation == UIDeviceOrientationPortrait || newOrientation == UIDeviceOrientationPortraitUpsideDown)) &&
((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight))
)
{
NSLog(#"Changed Orientation To Portrait");
self.orientationMode=#"portrait";
self.txtInsiderName.text=self.txtInsidernameLandscape.text;
self.txtCompanyName.text=self.txtCompanynameLandscape.text;
self.txtAddress.text=self.txtAddressLandscape.text;
self.txtPhoneLandscape.text=self.txtPhoneLandscape.text;
self.txtEmail.text=self.txtEmailLandscape.text;
// Clear the current view and insert the orientation specific view.
[self clearCurrentView];
[self.view insertSubview:mainPortraitView atIndex:0];
//Copy object states between views
//SomeTextControlP.text = SomeTextControlL.text;
}
orientation = newOrientation;
}
}
}
`
I need to show items in a combobox with a different background color. I also want to change what that color is depending on if the item is selected (or the mouse is on top of it), just the same way it works when a combobox is not owner-drawn.
It is all working fine, except that when the mouse comes off one of the items that I changed the color for, the item keeps the same color as when the mouse was on top. In the example below, the item 'other' is initially correctly drawn with myUnselectedBrush; the mouse goes over top, it is correctly drawn with mySelectedBrush; when the mouse comes off, it is incorrectly still drawn with mySelectedBrush; it should have been drawn with myUnselectedBrush. Everything works fine for item 'something', whose color is not altered.
What am I doing wrong?
private void comboBoxDraw(object sender, DrawItemEventArgs e)
{
ComboBox cb = (ComboBox)sender;
Graphics g = e.Graphics;
e.DrawBackground();
if (e.Index > -1)
{
object item = cb.Items[e.Index];
switch (somethingOrOther)
{
case something:
break;
case other:
e.Graphics.FillRectangle(
(cb.SelectedIndex == e.Index)
? mySelectedBrush
: myUnselectedBrush,
e.Bounds);
break;
}
}
}
e.DrawFocusRectangle();
if (e.Index > -1)
{
// draw the string
}
}
Rather than using
cb.SelectedIndex == e.Index
I needed to use DrawItemState:
((state & DrawItemState.Selected) > 0) || ((state & DrawItemState.HotLight) > 0)
I have a canvas in a scrollviewer.
To be able to use the scrollviewer, I've overridden the Canvas's MeasureOverride method to return the size of all children.
This works fine, except that the canvas can only contain items in positive space for the scrollviewer to work correctly.
I'd like the user to be able to drag elements around without the restriction of them having to be on the positive side of the canvas's origin.
So basicly I want to be able to position the elements anywhere, like at (-200, 10) or (500, -20) , and be able to scroll from the most left element (-200) to the most right(500), and from top to bottom.
To complicate matters the canvas can be scaled using the LayoutTransform, and I'd like to include the view-area in the scrollbars, so not only the min/max bounds of the children are taken into account by the scrollbars, but also the min/max of the current view-area.
Does anybody know how to make this work ?
Today I worked on this problem only :)
Happy to share the code which works:
public void RepositionAllObjects(Canvas canvas)
{
adjustNodesHorizontally(canvas);
adjustNodesVertically(canvas);
}
private void adjustNodesVertically(Canvas canvas)
{
double minLeft = Canvas.GetLeft(canvas.Children[0]);
foreach (UIElement child in canvas.Children)
{
double left = Canvas.GetLeft(child);
if (left < minLeft)
minLeft = left;
}
if (minLeft < 0)
{
minLeft = -minLeft;
foreach (UIElement child in canvas.Children)
Canvas.SetLeft(child, Canvas.GetLeft(child) + minLeft);
}
}
private void adjustNodesHorizontally(Canvas canvas)
{
double minTop = Canvas.GetTop(canvas.Children[0]);
foreach (UIElement child in canvas.Children)
{
double top = Canvas.GetTop(child);
if (top < minTop)
minTop = top;
}
if (minTop < 0)
{
minTop = -minTop;
foreach (UIElement child in canvas.Children)
Canvas.SetTop(child, Canvas.GetTop(child) + minTop);
}
}
Now call RepositionAllObjects method to realign the objects as and when required.
Of course, you need to have your own canvas derived from Canvas. And this method is required (you can tweak it if you like):
public static Rect GetDimension(UIElement element)
{
Rect box = new Rect();
box.X = Canvas.GetLeft(element);
box.Y = Canvas.GetTop(element);
box.Width = element.DesiredSize.Width;
box.Height = element.DesiredSize.Height;
return box;
}
protected override Size MeasureOverride(Size constraint)
{
Size availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
double minX = 900000; //some dummy high number
double minY = 900000; //some dummy high number
double maxX = 0;
double maxY = 0;
foreach (UIElement element in this.Children)
{
element.Measure(availableSize);
Rect box = GetDimension(element);
if (minX > box.X) minX = box.X;
if (minY > box.Y) minY = box.Y;
if (maxX < box.X + box.Width) maxX = box.X + box.Width;
if (maxY < box.Y + box.Height) maxY = box.Y + box.Height;
}
if (minX == 900000) minX = 0;
if (minY == 900000) minY = 0;
return new Size { Width = maxX - minX, Height = maxY - minY };
}
Now, all you need to do is wrap this canvas inside a scrollviewer.
Hope it helps!
I think the ScrollViewer assumes that the origin will be at (0, 0) -- after all, there's no way to tell it otherwise; Canvas is the only panel that knows about specified-and-nonzero origins.
A simple option might be to forget about a ScrollViewer, and implement your own panning functionality -- perhaps with dragging, or perhaps with a big "scroll left" button on the left side of the view, a "scroll right" button on the right, etc. -- and implement it in terms of a transform on the content.
But if you need to stick with the classic scrollbar metaphor, I think you could make your own panel (or override ArrangeOverride too, which amounts to the same thing) and offset all the positions -- make them zero-based. So if you have an element at (20, -20) and another at (0, 5), you would need to offset everything downward by 20 to make it fit in a zero-based space; and when you lay out your children, the first would go at (20, 0) and the second at (0, 25).
But now scrolling is weird. If the user is scrolled all the way to the bottom, and drags something off the bottom edge, the view stays put; same with the right. But if they're scrolled all the way to the top, and drag something off the top edge, suddenly everything jumps downward, because the ScrollViewer's VerticalOffset was zero before and is still zero, but zero means something different because of your layout offset.
You might be able to get around the scrolling weirdness by binding the HorizontalOffset and VerticalOffset. If your ViewModel automatically fixed up those properties (and fired PropertyChanged) whenever a control moves "negative", then you might be able to keep the view scrolled to the same logical content, even though you're now telling WPF's layout engine that everything is somewhere else. I.e., your ViewModel would track the logical scroll position (zero, before you drag the element negative), and would report the zero-based scroll position to the ScrollViewer (so after the drag, you would tell the ScrollViewer that its VerticalOffset is now 20).