Best place to call a method to adjust layout in iOS - layout

I am having a problem modifying my layout when switching from portrait to landscape. The layouts are as shown below. As you can see, the switch requires repositioning the two inner panels (light grey) relative to one another. The view controller is embedded in a navigation controller and the panels contain buttons which fire segues to further view controllers.
All the resizing is nicely taken care of by AutoLayout, but the repositioning requires using CGRect. On screen rotation is dealt with by
- (void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
self.topImagePanel.frame = CGRectMake(44, 16, 196, 392);
self.bottomImagePanel.frame = CGRectMake(44, 407, 196, 392);
} else {
self.topImagePanel.frame = CGRectMake(72, 64, 196, 392);
self.bottomImagePanel.frame = CGRectMake(268, 64, 196, 392);
}
}
And the following method deals with startup and navigating back to the home screen.
- (void) adjustLayoutToOrientation
{
UIInterfaceOrientation deviceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIDeviceOrientationIsLandscape(deviceOrientation)){
self.topImagePanel.frame = CGRectMake(72, 64, 196, 392);
self.bottomImagePanel.frame = CGRectMake(268, 64, 196, 392);
} else if (UIDeviceOrientationIsPortrait(deviceOrientation)) {
self.topImagePanel.frame = CGRectMake(44, 16, 196, 392);
self.bottomImagePanel.frame = CGRectMake(44, 407, 196, 392);
}
}
The most effective place to call this method from seems to be viewDidAppear. The problem is that whenever the view appears in landscape mode. It appears first as laid out in the storyboard (i.e. in portrait mode) for a brief interval before being replaced by the correct layout.
I have tried calling from viewWillAppear but although the method is called, the landscape layout simply never appears.I have also tried calling from viewDidLayoutSubviews. The landscape layout appears (when navigating back although not on start up) but with the same glitch as described above. One more thing I have tried is using different view controllers for the different layouts but it really seems to be overkill and causes more problems than it solves. Help appreciated.

Related

Setting (intern) variable for color

I'm trying to create a variable so I can use it across my app. I've put the "code" in OnVisible in my first screen with the following code:
Set(LayoutSettings, {
colorHR : RGBA(55, 207, 177, 1)
})
To test it, I simply used a rectangle and in Fill I use have:
LayoutSettings.colorHR
So basically I have:
HomeScreen.OnVisible = Set(LayoutSettings, {colorHR : RGBA(55, 207, 177, 1)})
Rectangle.Fill = LayoutSettings.colorHR
But somehow my rectangle is still black. What am I doing wrong?

JFreeChart Bar Charts - Draw plot without gradient fill

My Bar chart is always drawn with a gradient fill. I have a code to fill each bar in a series with a different color. I just want the colors without any effect. I looked at a previous thread but that did not help since it seems to be designed for a default standard code.
Here is my code:
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.WHITE);
CategoryItemRenderer renderer = new MyRenderer();
plot.setRenderer(renderer);
class MyRenderer extends BarRenderer
{
private Paint[] barColors;
public MyRenderer()
{
this.barColors = new Paint[] { new Color( 21, 104, 156), new Color( 25, 149, 104), new Color( 237, 179,20),
new Color( 72, 181, 163) };
}
public Paint getItemPaint(final int row, final int column)
{
// returns color for each column
return (this.barColors[column % this.barColors.length]);
}
}
Any help will be appreciated.
Thanks.
In the BarRenderer class you will find this method:
public static void setDefaultBarPainter(BarPainter painter);
It allows you to change the default bar painter (pass a StandardBarPainter to this method before creating any charts and your charts will have bars without the gradient).

MonoTouch UIButton Gradient Covers Title

I have a similar problem to this post (UIButton: Add gradient layer and title is not shown anymore - how to fix?) but his solution did not work for me. First things first, I'm somewhat new to MonoTouch, but have never gone as far as customizing the out of the box controls.
I am trying to add a gradient to my button, but when I do so, the gradient looks exactly as I want, but the text is covered (I set the opacity of my gradient to .5 to test). I have a storyboard in which I have visually designed the interface, but want to tweak a few things.
Here is my code:
var gradient = new CAGradientLayer();
gradient.Frame = getStartedButton.Layer.Bounds;
gradient.Colors = new MonoTouch.CoreGraphics.CGColor[]
{
UIColor.FromRGB (23, 55, 94).CGColor,
UIColor.FromRGB (33, 81, 141).CGColor
};
getStartedButton.Layer.AddSublayer(gradient);
getStartedButton.Layer.CornerRadius = 10;
getStartedButton.Layer.BorderColor = UIColor.White.CGColor;
getStartedButton.Layer.BorderWidth = 1;
getStartedButton.VerticalAlignment = UIControlContentVerticalAlignment.Center;
getStartedButton.Font = UIFont.FromName("Helvetica", 12);
getStartedButton.SetTitleColor(UIColor.White, UIControlState.Normal);
getStartedButton.SetTitle("Get Started", UIControlState.Normal);
Embarrassing, but it was simply a matter of changing the type of button to custom in XCode.

UIWebView keeps Portrait dimensions when viewed in Landscape

I have been unable to find a solution to this problem anywhere.
I am using Xcode 4.5 to write an iOS 6 app using storyboards. It uses a Navigation Controller to jump between several View Controllers with Navigation Bars, including a few that use a UIWebView to display a website. I have added code to the AppDelegate and various ViewController files to restrict the view to Portrait for most scenes in the app (since they really won't look good otherwise), but to allow Landscape view for the websites. And the Info.plist is configured allow all 4 orientations.
Everything works fine in the UIWebView scene; including rotation, scrolling and zooming - except for one thing: when I rotate from Portrait to Landscape, the horizontal dimension of the website stays locked at the horizontal dimension for the Portrait view - resulting in a large white space to the right of the website in the scene (see images below). I fixed a small (1/4") white space at the top of the UIWebView by selecting Layout: Wants Full Screen under View Controller in the Attributes Inspector. But I can't fix this.
I can supply relevant code or Storyboard settings if necessary. Can anyone help? Thank you.
I solved adding this code, when application loads on viewDidLoad method:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
Then add this to your view controller:
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
_displayWeb.frame = CGRectMake (0, 0, 768, 1024);
break;
case UIDeviceOrientationPortraitUpsideDown:
_displayWeb.frame = CGRectMake (0, 0, 768, 1024);
break;
case UIDeviceOrientationLandscapeLeft:
_displayWeb.frame = CGRectMake (0, 0, 1024, 768);
break;
case UIDeviceOrientationLandscapeRight:
_displayWeb.frame = CGRectMake (0, 0, 1024, 768);
break;
default:
break;
};
}
_displayWeb is your IBOutlet UIWebView, by the way this is using Xcode 4.6.1

SetWindowPos() function not moving window?

I have a dialog that I want to place within another dialog and position relative to one of the controls on the main dialog.
void CspAceDlg::DrawResultsArea()
{
CWnd* pTabCtl = GetDlgItem(IDC_BUILDTABS);
CRect rectTabCtl; // Allocate CRect for control's position.
pTabCtl->GetWindowRect(&rectTabCtl);
int resX = rectTabCtl.right + 15;
int resY = rectTabCtl.top;
//RESULTS AREA
results.Create(IDD_RESULTSDIALOG, this);
results.SetWindowPos(this, resX, resY, /*608, 19, */175, 135, SWP_SHOWWINDOW);
results.ShowWindow(SW_SHOW);
}
My problem is that my dialog resource (IDD_REULTSDIALOG) has properties called X Pos and Y Pos that seem to be overriding my SetWindowPos() (and the little property tab in the resource editor won't let me leave these blank). If I set these properties to 0, 0 my dialog appears in the top left corner of the main dialog. If I set them to a number I can guess-and-test place it roughly where I want, but then running the application on different resolutions causes the dialog to appear in different spots. What I really want to do anyway is place the dialog relative to another control on my main dialog (in this case my tab control). Why is my SetWindowPos() being ignored, and how do I fix this? Should I be using a different function?
According to the documentation for SetWindowPos, if you pass in SWP_SHOWWINDOW, the window will not be moved:
If the SWP_SHOWWINDOW or SWP_HIDEWINDOW flag is set, the window cannot be moved or sized.
Figured it out myself, largely due to this thread.
My code came out looking like this:
void CspAceDlg::DrawResultsArea()
{
CRect rectTabCtl; // CRect representing tab control's position.
POINT pResXY;
POINT pResWH;
CWnd* pTabCtl = GetDlgItem(IDC_BUILDTABS);
pTabCtl->GetWindowRect(&rectTabCtl);
pResXY.x = rectTabCtl.right + 15;
pResXY.y = rectTabCtl.top;
pResWH.x = pResXY.x + 175;
pResWH.y = pResXY.y + 135;
ScreenToClient(&pResXY);
ScreenToClient(&pResWH);
//RESULTS AREA
results.Create(IDD_RESULTSDIALOG, this);
//results.SetWindowPos(this, resX, resY, /*608, 19, */175, 135, SWP_SHOWWINDOW);
results.MoveWindow(pResXY.x, pResXY.y, pResWH.x, pResWH.y, TRUE);
results.ShowWindow(SW_SHOW);
}
What fixed this problem for me, was setting the program's compatibility properties to "run this program as an administrator".

Resources