UIProgressView defaulting to top left corner of main view - iphone

My UIProgressView is going to the top right hand corner of the main view - even though I set it as a subview of a UIView (which in turn is a subview of the main view). And even if I setFrame:.
Any ideas?
if(downloadBar == nil){
downloadBar = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 10, 200, 10)];
}
[downloadBar setProgressViewStyle:UIProgressViewStyleDefault];
[downloadBar setProgress:([download downloadStatus]/100.0f)];
[downloadView addSubview:downloadBar];
// download view initialisation
// this is initalised BEFORE the download bar
downloadView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
[downloadView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:downloadView];
[downloadView retain];
Thanks

I think it depends on your downloadView. Because the origin coordinate (0, 0) of downloadBar will be at the same position of the origin coordinate (0, 0) of the downloadView. So, if you downloadView is located in top right, the downloadBar will be the same

Related

Subview appears underneath superviews layer.border?

I have a UIView in which I define it's border in the following manner:
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 3;
I attach a subview to this UIView, and when I move the subview over the border, it goes underneath it. Is this the intended behavior? Is there anyway to make the subview go on top of it?
According to the Apple specification: It is composited above the receiver’s contents and sublayers.
So, the border will always be above of all your subviews, even if you bring your subview to the front and so on.
So I make a background view to fake the border.
E.g.:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.clipsToBounds = NO;
UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
bView.backgroundColor = [UIColor redColor];
UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
cView.backgroundColor = [UIColor yellowColor];
[bView addSubview:cView];
[backgroundView addSubview:bView];
[self.window addSubview:backgroundView];
and the effect:
Depending on your view structure, it might be easier to add the subview to the parent of your main view. It can then overlap the main view and will overlay the border as you requested.
Did you try setting the superview's 'clipsToBounds' property to YES? This is set to NO by default for performance reasons, but setting it to yes might give you the effect you are looking for.
Insert layer at specific position that suits you:
self.layer.insertSublayer(sublayer, at: 0)

How to fix UILabel alignment in UINavigationBar

I put a custom UILabel in my UINavigation bar like this:
UILabel *navTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
navTitle.backgroundColor = [UIColor clearColor];
navTitle.text = #"TEST";
navTitle.font = [UIFont fontWithName:#"GothamNarrowBook-Regular" size:28];
navTitle.textColor = [UIColor whiteColor];
navTitle.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navTitle;
But when it shows up on the emulator, its aligned too high:
I have had no luck with adjusting the frame. Any ideas?
You can put label inside another view, and insert this another one as titleView. That will allow some kind of flexibility.
You can also try to adjust position of the frame:
UILabel *navTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, >>>10<<<, 200, 44)];
But remember, that NavigationBar has different height in landscape mode, so you have to use autoresizingMask accordingly.
Can you set the frame of self.nagivationItem.titleView and push it down?
CGRect initialFrame = self.navigationItem.titleView;
self.navigationItem.titleView = CGRectMake(initialFrame.origin.x, initialFrame.origin.y+10, initialFrame.size.width, initialFrame.size.height);
Another thing to check, make sure your UINavigationBar is actually 44px tall, if your UILabel is the same height then I believe default behavior is to center vertically which would land it in the correct position.
I take it that increasing the height of the UILAbel doesn't work.
Or the Y position.
And I take it you tried adjusting the frame after it's created, e.g. other than in the initWithFrame.
Maybe try using an image instead? Well it's an idea ;)

How to make UIScrollView respect the layout of the containing UIView?

I'm using a UIView to control the layout of my view (along with a view controller). I want UIScrollView to only use half of the vertical screen. That works fine if I use the upper half of the screen, but not the bottom half.
Here's the relevant code from the UIViewController:
- (void)loadView {
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame];
//trying to put the scroll view on the bottom half of the screen, but does not work.
CGRect halfFrame = CGRectMake(0, fullFrame.size.height / 2 ,
fullFrame.size.width, fullFrame.size.height / 2);
//use this instead for the scroll view to go to the top half of the screen (and work properly)
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height / 2);
UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame];
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)];
CGRect stencilFrame = halfFrame;
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame];
stencilFrame.origin.x += stencilFrame.size.width;
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame];
//mix up the colors
[leftView setBackgroundColor:[UIColor redColor]];
[centerView setBackgroundColor:[UIColor greenColor]];
[rightView setBackgroundColor:[UIColor blueColor]];
//add them to the scroll view
[sv addSubview:leftView];
[sv addSubview:centerView];
[sv addSubview:rightView];
//turn on paging
[sv setPagingEnabled:YES];
UIView *containerView = [[UIView alloc]initWithFrame:fullFrame];
[containerView addSubview:sv];
[self setView:containerView];
}
Thank you in advance for any advice or help.
I figured it out. The crux of the problem is that views within the scroll view are initialized with the same frame as the scroll view itself. When the scrollView is initialized with halfFrame, the origin is (0, half the full screen size), which is ok since that is relative to the application window itself. However, the views that are put inside the scrollView (like leftView) are initialized to halfFrame, but in this case the origin is relative to the scrollView, effectively placing them off the screen. Setting the origin to (0,0) fixes this:
CGRect stencilFrame = CGRectMake(0, 0, fullFrame.size.width , fullFrame.size.height / 2);
contentSize must contain the rectangle of the view inside the scroll view. That is, the total size of all scrollable controls within. The frame of the UIScrollView decides how much scrolling is needed to let the user browse everything.
You don't have the "full frame" available if you have a nav bar or a tab bar. In general, code that uses [UIScreen mainScreen] for layout information is probably wrong.
Additionally, the status bar can change size if (for example) a call is in progress or tethering is enabled.
Instead, use any sane value for full frame and enable autoresizing:
CGRect fullFrame = {{0,0}, {320,480}};
...
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
EDIT: You also probably need to subclass UIScrollView and implement -setFrame: so that it also sets the content size and -layoutSubviews to do the correct layout.

Landscape Screen shifted to right in 20px

I am trying to rotate the screen on the iphone. For example,
First state of screen : is "Portrait".
Then it calls "Landscape" screen. Following code is used for making Landscape screen :
- (void)viewDidLoad {
[super viewDidLoad];
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
CGFloat angle = 90 * M_PI / 180;
self.view.transform = CGAffineTransformMakeRotation(angle);
self.view.center = [nRangeAppDelegate sharedAppDelegate].window.center;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 480, 44)];
label.textAlignment = UITextAlignmentLeft;
label.numberOfLines = 2;
label.text = #"12345\n67890";
[self.view addSubview:label];
}
But above code shifted to right in 20 px.
How to make 480x320 view at point 0,0 no shifting ?
Thanks in advance.
Update:
My application navigation based application. All UIViewControllers have one UIView. When call the the view I am trying rotate, this code is used : MyRoratedController *myCtrlr = [[MyRoratedController alloc] initWithNibName:#"MyRoratedController" bundle:nil]; [navigationController pushViewController: myCtrlr animated:NO]; I've changed CGPointMake(160.0, 240.0) by many different values. But no changes.
Your window is shifted because the appDelegate window is not centred on the screen as it is drawn below the status bar
Try this:
self.view.center = CGPointMake(160.0, 240.0);
You must make sure you are adding this view to a normal viewcontroller (ie not a nav controller or similar). The easiest way to do this is to add it to the main application window, like so:
MyRoratedController *myCtrlr = [[MyRoratedController alloc]
initWithNibName:#"MyRoratedController" bundle:nil];
[window addSubview:[myCtrlr view]];
Note that if your app has a status bar shown you will have to change the 160 above to 150

UIScrollView starting point

I have a iPhone app with a main view that is 480 x 510. I'm a bit confused as to how the coordinate system changes when I go from portrait to landscapeRight.
Initially, I had a 480x480 content area, and wanted to add a button on the side to bring up a menu. However, the only way I could find to have the writing run down the length of the left side of the app was to change the orientation of the whole app to landscape right.
Now, with the orientation in Landscape, the scrollview is starting at the top left, when I really want it to start at the original orientation point (from when it was in portrait mode), which would be bottom left. But I can't figure out how to change the original starting point of the scrollview. I tried contentOffset, but it doesn't seem to be working.
Another way of going about this (that I might end up doing) is forget having a title on the button, and just create a graphic for the button that has text vertically oriented.
if (self = [super initWithCoder:coder]) {
// Size of the scrollview
self.contentSize = CGSizeMake(480, 510);
[self setContentOffset:CGPointMake (0, -160)];
self.showsHorizontalScrollIndicator = YES;
self.showsVerticalScrollIndicator = YES;
self.alwaysBounceVertical = NO;
self.alwaysBounceHorizontal = NO;
self.bounces = YES;
self.userInteractionEnabled = YES;
self.musicGridView = [[[MusicGridView alloc] initWithCoder:coder] autorelease];
// Rectangle where I put the music grid (origin is left aligned, and down 160 pixels to account for landscape view)
self.musicGridView.frame = CGRectMake(0, 160, 480, 480);
[self addSubview: musicGridView];
self.menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.menuButton.frame = CGRectMake(0, 480, 480, 32);
[menuButton setTitle: #"Tap to get Menu" forState: UIControlStateNormal];
[menuButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[menuButton addTarget:self action:#selector(showMenu) forControlEvents:UIControlEventTouchUpInside];
[self addSubview: menuButton];
simply set the points x,y in
//it goes in particular pont of view
[scrollview setContentOffset:CGPointMake(x,y) animated:YES];
//if x=0 in loop, increase y value scrollview move only vertical direction
[scrollview setContentOffset:CGPointMake(0,y) animated:YES];
//if y=0 in loop, increase x values scrollview move only horizontal direction
[scrollview setContentOffset:CGPointMake(x,0) animated:YES];
Andrey Tarantsov has done an awesome rundown of the various quirks of the UIScrollView class, including code for various workarounds to common problems. It might be worthwhile taking a look.