Xcode Label not centered - objective-c

Programmatically created a scroll view then added a label.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:scrollView];
then label
UILabel *errLabel = [[UILabel alloc] init];
errLabel.text = #"Top Label";
//errLabel.frame = CGRectMake(self.view.frame.size.width/2,50,120,20);
errLabel.frame = CGRectMake(CGRectGetWidth(scrollView.frame)/2.00,50,120,20);
errLabel.textAlignment = NSTextAlignmentCenter;
errLabel.layer.borderColor = [UIColor grayColor].CGColor;
errLabel.layer.borderWidth = 3.0;
[errLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:14]];
[scrollView addSubview:errLabel];
In the simulator for iPhone 8, 11 Pro Max, and biggest iPad the label is right of center. Is this an iOS 11 bug or am I missing something?

This is because the way you are creating the frame for your label, you are starting the frame at the mid point of the scroll view.
This will offset the starting X point to center your label:
errLabel.frame = CGRectMake(CGRectGetWidth(scrollView.frame)/2.00 - 60,50,120,20)

Related

Admob banner change frame for different screen dimensions

I'm trying to get my Admob ad to adjust between all the different iPhone screen sizes that Apple offers, but I can't figure out how to get it to adjust automatically or by using different code.
In my viewdidload
bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
bannerView_.adUnitID = #"";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:#"", nil ];
You need to set the adSize property to Google's set banner size kGADAdSizeBanner. Then you set your GADBannerView's frame relative to the view. The following code places the GADBannerView on the bottom of your view.
admobBannerView.adSize = kGADAdSizeBanner;
admobBannerView.frame = CGRectMake(0, self.view.frame.size.height - admobBannerView.frame.size.height , self.view.frame.size.width, admobBannerView.frame.size.height);
x - Set to origin of 0
y - Set origin to the view's height. But this will make it appear off the bottom of the screen. So, we must subtract the height of our GADBannerView to move it up onto the screen: self.view.frame.size.height - admobBannerView.frame.size.height
width - Set it to the width of the view: self.view.frame.size.width
height - Set it to the height of our GADBannerView: admobBannerView.frame.size.height
Banner Ad Customization
Since you are creating the banner programmatically instead of with Storyboard, I would check to find out what the height of the screen is. I use the following code to determine what phone someone is on.
CGFloat greaterPixelDimension = (CGFloat) fmaxf(((float)[[UIScreen mainScreen]bounds].size.height), ((float)[[UIScreen mainScreen]bounds].size.width));
I would then create the bannerview in the code like this:
if (greaterPixelDimension == 736) { //6 plus
bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, ###, 4--, 50)];
}
else if (greaterPixelDimension == 667){ // 6
bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, ###, 3--, 50)];
}
else if(greaterPixelDimension == 568){ // 5
bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
}
etc. (obviously replace the ### and 3-- with the respective points, I forget the numbers off the top of my head)

iOS8 UIButton title disappear

I have a button in my app that is perfectly visible on iOS7. But when I switch to iOS8 the title label for this button does not appear. And I can see only border around the button.
Here is the code that makes the button. Could someone give me a clue where the problem is?
UIButton *problem = [[UIButton alloc] initWithFrame:frame];
[problem addTarget:self action:#selector(problemButton:) forControlEvents:UIControlEventTouchUpInside];
problem.backgroundColor = [UIColor whiteColor];
[problem setTitle:NSLocalizedString(#"ReportAProblem", nil) forState:UIControlStateNormal];
problem.titleLabel.textColor = CUSTOM_DARK_GRAY_COLOR;
[[problem layer] setBorderWidth:1.0f];
[[problem layer] setBorderColor:CUSTOM_DARK_GRAY_COLOR.CGColor];
problem.layer.cornerRadius = 5;
problem.clipsToBounds = YES;
I found an answer for this question but immediately there is another:
I encounter the very same problem with some of my UILabels, for example:
self.label = [[UILabel alloc] initWithFrame:labelFrame];
self.label.numberOfLines = 1;
self.label.adjustsFontSizeToFitWidth = YES;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.textColor = self.textColor;
CGFloat fontSize = self.bounds.size.height / 5;
self.label.font = [UIFont systemFontOfSize:fontSize];
You should use - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state instead of titleLabel.textColor.
Second problem: Why are you trying to set a ratio of your view height as a font size ? Maybe doing it with your label makes more sense. CGFloat fontSize = self.label.bounds.size.height / 5; Anyway what you usually want to do is have a fixed font size and adapt the label frame around it.

How to disable vertical scrolling in UIScrollView (Obj-C)

I want to disable vertical scrolling from my UIScrollView if possible.. My code is like below.. Working fine except users can scroll up and down which shouldn't be there I believe.. Thanks in advance..
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 3)];
scroll.contentSize = CGSizeMake(scroll.contentSize.width,scroll.frame.size.height);
scroll.pagingEnabled = YES;
scroll.backgroundColor = [UIColor blackColor];
int xVal = 30;
NSInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++) {
UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 0, 90, 100)];
UILabel *testLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 20, 90, 100)];
UILabel *testLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 40, 90, 100)];
testLabel2.backgroundColor = [UIColor clearColor];
testLabel2.text =#"Test1";
testLabel2.textColor = [UIColor whiteColor];
testLabel2.font = [UIFont boldSystemFontOfSize:12];
testLabel1.backgroundColor = [UIColor clearColor];
testLabel1.text =#"Test2";
testLabel1.textColor = [UIColor whiteColor];
testLabel1.font = [UIFont boldSystemFontOfSize:12];
testLabel3.backgroundColor = [UIColor clearColor];
testLabel3.text =#"Test3";
testLabel3.textColor = [UIColor whiteColor];
testLabel3.font = [UIFont boldSystemFontOfSize:12];
xVal += 120;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(xVal, 30, 150, 130)];
view.backgroundColor = [UIColor blackColor];
xVal += 200;
[scroll addSubview:testLabel1];
[scroll addSubview:testLabel2];
[scroll addSubview:testLabel3];
[scroll addSubview:view];
}
[self.view addSubview:scroll];
In my situation, I was unable to get the height of the scrollview (due to autolayout, I wasn't able to get the height in viewDidLoad). You can add this to the delegate method.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}
you must set your scrollview content height to the scroll view height
CGSize scrollableSize = CGSizeMake(scrollableWidth, yourScrollViewHeight);
[myScrollView setContentSize:scrollableSize];
Here may be a possible duplicate
disabling vertical scrolling in UIScrollView
or you can also try this:
self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * number_of_items, 1);
Assuming it's an iPhone app, so the screen resolution is 320×480 .
Now you are setting your scroll view's height as self.view.frame.size.height / 3 .
Here your view's height is actually taken as 460 and not 480 (20px for staus bar).
So when you add the other view as subview to your scroll view, its frame goes out of the scroll's content view. So you need to manage this while setting your frames/content size.
Let me know if this works for you.
There is no problem with that simply change the contentSize of your UIScrollView and you are done.Increase its width size and its height should be as it is at present.Moreover you can also hide the vertical scrollers also.
scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake(scroll.contentSize.width + xVal,scroll.frame.size.height);
You should do like this:
aScrollView.scrollsToTop = NO;
aScrollView.delegate = self;
aScrollView.contentSize = CGSizeMake(aScrollView.frame.size.width * X, aScrollView.frame.size.height/2);
In your xml file there are two properties are available for scrollview are horizontal scroll and vertical scroll. as per your requirement you can check or uncheck and if you want to stop vertical or horizontal scroll then you have to make same content size of scrollview with height or width of scrollview respectively

Extending UIScrollView when it fills up with more content

Ok, so i got a scrollview to load when an user logs in to my application. When the user is authenticated, i need to create a scrollview programmatically. To do so, i have the following code:
[scrollView removeFromSuperview];
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, self.view.frame.size.height)];
view.backgroundColor = [UIColor greenColor];
//-- get the y-coordinate of the view
CGFloat viewCenterY = self.view.center.y;
//--calculate how much visible space is left
CGFloat freeSpaceHeight = applicationFrame.size.height - keyboardBounds.size.height;
//-- calculate how much the scrollview must scroll
CGFloat scrollAmount = viewCenterY - freeSpaceHeight / 2.0;
if(scrollAmount < 0) scrollAmount = 0;
//set the new scrollsize contentview
view.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + keyboardBounds.size.height);
//scroll the scrollview
[view setContentOffset:CGPointMake(0, scrollAmount) animated:YES];
for(int v = 0; v < 15; v++){
CGFloat yCoord = v * 100;
UILabel *mijnLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, yCoord, 200, 50)];
mijnLabel.text = #"1337";
mijnLabel.font = [UIFont fontWithName:#"Verdana" size:20];
mijnLabel.textColor = [UIColor redColor];
[view addSubview:mijnLabel];
[mijnLabel release];
}
[self.view addSubview:view];
[view release];
}
How do i extend my view according to the amount of labels i added? I also want to know how i can make my scrollview fill up the entire screen.. My scrollview is now only filling up the screen when in portrait mode.
This is the login screen that gets removed:
[scrollView removeFromSuperview];
you need to keep track of numbers of label that you are adding into the scroll view. suppose if your label's height is 50 then take a variable and make a total of all label's height and give a new frame to your scroll view. like :
view.frame = CGRectmake(0,0,applicationFrame.size.width,totalHeightOfLabel);
[self.view addSubview:view];
[view release];
hope this will give a idea...

can't set tableview frame in landscape, parameters set but view is strange [screenshot attached]

I try to set the frame of a UITableView to (190,0,610,768) in landscape mode. The frame parameters are correctly set but display is not correct.
To contrast the result, I added a UIlabel with same frame to the bottom:
CGRect frame = CGRectMake(190, 0, 610, 768);
UILabel *lbtb = [[UILabel alloc] initWithFrame:frame];
lbtb.backgroundColor = [UIColor cyanColor];
lbtb.text = #"";
[self.view addSubview:lbtb];
dishMenuVC = [[DishMenuViewController alloc] init] ;
dishMenuVC.view.frame = frame;
dishMenuVC.view.backgroundColor = [UIColor redColor];
dishMenuVC.view.alpha = 0.5;
[self.view addSubview:dishMenuVC.view];
I found a workaround to this problem by creating a container UIView with my intended frame and add the tableview to its subview.