IOS6 Constraints using Autolayout - ios6

I have an app that can be rotated, so I need to deal with portrait and landscape orientations. Additionally, users will be allowed to use pinch gestures to change the scale of views. Here is the basic hierarchy of the views.
mainView is a subview of self.view (from the context of the main view controller). It is a UIImageView, although the image part of it is relatively unimportant. In any case, this is the view within which the rest of the views in this discussion are placed as subviews.
The first is what I call the board. It is the view on which items are assembled by the user. These items are themselves image views.
Additionally, there are what I call palettes. These are simply views that can be resized and scaled by the user. Additionally, the image views just mentioned can be dragged from one palette to another or to the board. The palettes can be thought of as work space for the user. When they are finished their work, they place their assembly onto the board.
So far, I've been working with the app where the board is part of autolayout but the palettes are created programatically as needed. This is good because when the user rotates the device, autolayout automatically places the board appropriately. At least it did until I wanted to add pinch scaling to it.
Autolayout has the following constraints set to it in interface builder:
Leading, top, trailing, and bottom all set to superview default.
When the user scales the view, the result is that it sort of sticks to the upper-left corner of the screen. I'd rather have it retain the center.
I tried changing this programmatically by adding the following code to the pinch gesture recognizer for this view:
if (self.pinchView.tag == TAGBOARD) {
[NSLayoutConstraint constraintWithItem:self.pinchView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.mainView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
}
but this seemed to do nothing. I'm guessing it's because it conflicts with the IB constraints. Is there something else I can do to make this work with autolayout? Or should I just do it all programmatically like I do with the other views?
In this code example, self.pinchView is the view on which the pinch gesture is applied to. For the sake of this discussion, it is what I've called the board. The self.mainView view is its superview.

The part we're not seeing in the code is something like
[self.pinchView addConstraint:yourNewConstraint];
I can see where you create the constraint, but not add it to the view. If you want your constraint to win you'll need to remove the other views or make sure the new constraint has a higher priority.
If your view should be centered, try adjusting the constraints in the storyboard to pin the width and height to the default and then aligning horizontally in the center. That should satisfy autolayout and replicate what you're trying to add, then in your pinch recognizer you can change the constant of the width and height. Be sure to drag your width and height constraints into your controller to create an outlet so that you can adjust them during the pinch gesture.

Related

Codename One app shown in a centered rectangle on wide screens

Note for the readers: this question is referred exclusively to Codename One.
Because I have multiple targets for the same app (Android, iOS, Javascript), that can be run on small screens (that is the typical use case) but also on wide screens, I have to do a choice about the UI.
I want to simplify my life: mobile devices held on portrait mode are my primary target, so... all wide screens (desktop and tablet) will show the app inside a vertical rectangle in the center of the screen. Maybe this is not the best solution, but it can make sense. I suppose that the width of that centered rectangle is 9 cm (my iPhone device width is about 7 cm and my Android device width is about 8 cm).
So... what is a proper way to accomplish this requirement?
Initially I thought that I can create a BaseForm with a code that override the show() method, setting a left and right margin for ContentPane, LayeredPane and Toolbar before calling super.show()... but this solution seems to me a poor way to go and also an overhead, because in that case I'll need to recalculate the margins and to revalidate the BaseForm for every change of size of screen... and, however, this solution will not work with FABs or Dialogs. So... it's not a solution.
It could be nice if I can choose a background (an image or a color) for all the space outside the centered rectangle containing the app.
Any better idea?
This is probably something that we need to implement in the system to make it seamless. Any approach you choose will require some work.
If I were doing something like this I'd create a special form with no title that acts as your background. I'd give it a special custom layout manager that centers the content. Something like:
public CenterOneCompnentLayout class Layout {
public void layoutContainer(Container parent) {
if(parent.getComponentCount() == 1) {
Component cmp = parent.getComponentAt(0);
cmp.setWidth(convertToPixels(9));
cmp.setHeight(parent.getHeight());
cmp.setY(0);
cmp.setX(parent.getWidth() / 2 - cmp.getWidth() / 2);
}
}
public Dimension getPreferredSize(Container parent) {
return new Dimension(100, 100);
}
}
Now just add the actual form to this background form. The rest should work "as is". I think you would need to guard against some things and it's possible this hack will produce some side effects due to the nesting of the forms. But ultimately it should work.

Rotate WPF control or change screen orientation

I have wpf project with one Window (MainWindow). Depending upon the config file it shows one of two UserControl's as Content. It may be a horizontal (1920x1080) control or vertical (1080x1920) control. It's fine with horizontal screen, but when vertical is loaded I would like to do:
1) rotate window/control by 270 degrees
2) change primary screen orientation
I would prefer to just rotate application and don't interact with windows API. I can't change orientation manually, because I have only remote access to this computer.
You can not rotate the Window object itself, as it is positioned by the window management system built in Windows. You can, however, transform (and thus rotate) any FrameworkElement inside the window. This includes, but is not limited to, the Grid, the Button and the TextBox elements.
All you need to do is edit the LayoutTransform property on the element you want to rotate, which is most likely the root element in your window. Set the rotation to 270/-90 degrees and WPF will automatically rotate your UI.
Because you are using the LayoutTransform property, the layout system will also scale you UI correctly. The RenderTransform property causes the control to first be rendered, then be rotated.
YES WE CAN CHANGE SCCREEN ORIENTATION USING
DEVMODE & using System.Runtime.InteropServices;
its bit late to reply but I am replaying for the new ones , if someone com across this article for change screen rotation in C# or VB .
Please use the link given below to get help Mr. Hannes Completely write an article to change screen rotation and luckily its working fine for me (Windows 11) as now of..
https://www.codeguru.com/dotnet/creating-a-screen-rotator-in-net/

How to make UIScrollView load data as scrolling happens

I have an IOS application designed for ipad. In one page, I display a report to the user and the report requires 5000 UILabels to be rendered on the screen inside a UIScrollView. This causes application to crash due to being out of memory. I know that UITableView has the functionality for loading content on demand. How can I make scroll view render only certain content and as the scrolling happens remove the content that got invisible and add the content that should be visible?
Is there any way you can reconfigure it to use a table view with custom rows, perhaps rows that consist of several labels? That model supports load on demand and resource reuse in a very natural way...
Perhaps you can be more specific on your screen layout?
You should re-cycle (i.e., reuse) the labels just like a table view recycles its table cells. This is sometimes referred to as "tiling" subviews. Tiling allows you to display more than will fit into memory.
I recommend watching WWDC 2012 Session 104. This session's tutorial creates a photo app that tiles image views in a UIScrollView. Although the photo app scrolls pages of content, rather than a grid of items, I think the video could be relevant to your app.
Here's a very brief overview of the tutorial applied to your specific case:
(1.) declare iVars that keep track of your labels:
NSMutableSet *recycledLabels;
NSMutableSet *visibleLabels;
(2.) implement a method that fetches a re-usable label:
- (UILabel *)dequeueRecycledLabel;
(3.) implement a method that does the tiling:
- (void)tileLabels; // this will add/remove labels from the scroll view
(4.) set your scroll view's delegate and call tileLabels in the scrollViewDidScroll: delegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self tileLabels];
}
The WWDC video will help you fill in the details, which I think are applicable to your case. However, you will have to do plenty of improvising in order to make it work for your specific case. This may not be easy; but its doable.
At the end of the video, tiling with CATileLayer is discussed. I'll be honest, I didn't understand that part. But I don't think it's applicable in your case because you're not displaying large images.
I finally implemented dynamic content loading. The method I implemented is scrollViewDidScroll.
Inside this method I determined the direction of scroll by the following
bool isUp = (currentOffset.y > lastScrollOffset);
Then I determined the visible content rectangle by the following.
CGRect visibleContentRect;
visibleContentRect.origin = scrollView.contentOffset;
visibleContentRect.size = scrollView.bounds.size;
Then I had an array of View Elements and each knew its place in the scroll view because of their frame being set. Long story short, each time scroll happened, I determined the views whose frame either intersected or contained by visible content frame. I added those views to the scroll view. I also determined the ones that disappeared and removed them from scroll view and I also set those views to nil and recreated them. Once [scrollView addSubview:view] method is called, then view gets more space in the memory because it gets visible. [view removeFromSuperView] method doesn't deallocate that space. That's why setting the view to nil and recreating it is necessary.

How to rearrange views with iOS Auto Layout

I'm trying to wrap my head around auto layout. I think I get it for the most part (probably missing something), but how would i set up the views pictured below?
Without auto layout, I would just test for orientation and then re-set the frames of the blue and purple views (and also the white ones probably). In auto layout, I'm not sure yet how to approach this. Would I need to remove and replace the constraints, so that in top/bottom constraints between these views in portrait orientation, I would have leading/trailing between them in landscape? Or is it easier than that?

WPF, any easy way to work with different screen resolutions?

Given a WPF Application running full screen, a fair amount of controls some of which will animate from off screen to center. I was wondering if there are any special ways to save on the amount of time required to optimize an application for different screen resolutions?
For example, using Blend I've setup some text, which is originally off screen to scroll into view. Now in design mode the start positions are static. If resolution changes the start positions will obviously not be correct.
So I guess to correct this, during app startup. I need to
Check resolution
Move the text box to the desired start location
Adjust the storyboard as required, so the frames all have correct co-ordinates depending on the res of the screen.
I don't mind doing all of this, but if there is an easier way please share!
Thanks
In WPF layout of controls should be made in such way, that when size of window or content changes, controls automaticaly resize/reposition themselves to reflect this change.
This is highly affected how your layout is made, especialy by using specific panels, that do layout of their child elements.
This is also made obvious by using device-independent units for sizes, margins and sometimes positions of controls. And also allows different kind of zooming and scaling of whole UI without any need to redesign the whole thing.
If you are trying to position your controls absolutely, either by using Canvas panel or margins, your are doing it totaly wrong.
In WPF, scene is measured in abstract units, not pixels, and controls are freely scaled. There should be no problems to center something, or what?

Resources