UISlider frame error on ios7 - frame

UISlider frame min width is 22, min height is 34.
Even if i set frame CGRectMake(0,0,1,1) it cannot change correctly.
UISlider *slider = [[UISlider alloc] init];
slider.frame = CGRectMake(0,0,1,1)

I think, that Apple make 34 as minimum size of UISlider. Not 42, but 34
Try code:
UISlider *slider = [[UISlider alloc] init];
slider.frame = CGRectMake(0,0,36,36)

Related

Xcode Label not centered

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)

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)

UILabel won't resize

I have UILabel in my View that should resize itself based on the content, but it doesn't do so. I have multiline content in it. I tried [label sizeToFit] + label.numberOfLines= 0.
I don't have constraints for the UILabel.
I also tried to use UITextView instead, but there the font just stays on 13 and not as I want on 17. Can you help me?
This is the code that works now:
UILabel *textLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 158, self.view.bounds.size.width-40, 550)];
textLabel2.text = textString;
textLabel2.font = [UIFont fontWithName:#"Helvetica Neue" size:17];
textLabel2.numberOfLines = 0;
[textLabel2 sizeToFit];
[self.scroller addSubview:textLabel2];
If you are creating your UILabel programmatically:
This code first sets the UIlabel's text then get the width of the text
UILabel *label = [[UILabel alloc] init];
//set label backgroundColor so you can see the label width changes according to text
[label setBackgroundColor:[UIColor redColor]];
//edit you label text
[label setText:#"very longgggg text"];
//get width of you text and set you font size to 17.0
CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:17.0f]].width;
//set you label frame
label.frame = CGRectMake(10, 200, width, 30);
//add label to you view
[self.view addSubview:label];
If you're not creating you UILabel programmatically remove the alloc init line and connect you outlets to you xib or storyboard.

UIImageView show image out of bounds in a UICollectionViewCell sub class

I have to create a customize UICollectionViewCell to show a image and a name for the image, And on top of the image, there is a frame image which is 2 pixels bigger in width and height than the image. However, no matter what I do, the image seems to be bigger than the frame image. I did the same thing for the table view, and it works perfect.
Here is the code:
//GridCell.h
#interface GridCell : UICollectionViewCell
#property(nonatomic, strong) UILabel *lblName;
#property(nonatomic, strong) UIImageView *image;
#end
//GridCell.m
#import "GridCell.h"
#implementation GridCell
#synthesize image, lblName;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *bg = [UIImage imageNamed:#"borderUIimgLg.png"];
UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.width)];
[bgImage setImage:bg];
[bgImage setContentMode:UIViewContentModeScaleAspectFill];
NSLog(#"BG Image size %f, %f", bgImage.frame.size.width, bgImage.frame.size.height);
UIImageView *contentImage = [[UIImageView alloc] initWithFrame:CGRectMake(2.0, 2.0, frame.size.width-4.0, frame.size.width-4.0)];
[contentImage setContentMode:UIViewContentModeScaleAspectFill];
[contentImage setClipsToBounds:YES];
self.image = contentImage;
[self.contentView addSubview:self.image];
[self.contentView addSubview:bgImage];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2.0, frame.size.width, frame.size.width - 4.0, 21.0)];
[label setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:11.0]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
self.lblName = label;
[self.contentView addSubview:self.lblName];
}
return self;
}
The UICollectionViewCell has a size of 67 x 100, so in the code, the bgImage supposed to be always to be 67 x 67 and its origin is (0,0) and the contentImage supposed to have a frame of (0,0,63,63). By debugging, it seems correct. However, the conentimage always bigger than the bgImage. The original size of the image is 80 x 80 though. I have tried setClipToBounds,
setContentViewMode on either the cell.ContentView or the imageView, but none works.
A screenshot about the problem is attached.
Any help is appreciated.
This might have found the answer by now, try:
contentImage.clipsToBounds = YES;
you are using 2 times frame.size.width instead of frame.size.height on the other one
edit, try this:
in the cell, when using initWithFrame methods, use the self.bounds property of the cell.
if you init the imageview inside the border use CGRectInset method to initialize the imageview with smaller bounds and set the imageviews center to same as the contentview of the cell
Try changing UIViewContentModeScaleAspectFill to UIViewContentModeScaleAspectFit.

UISlider in UIToolbar -> can't get slider.value and process it. iOS SDK

I'm trying to get a UISlider into my UIToolbar and to take the value from it and process it. I also want to be able to change the slider.value via other methods.
So, I set it up for the UISlider by it's self (not on a UIToolBar) and everything worked fine. Then, when I put it into the UIToolBar, the value stopped getting sent to and from it (I am NSLogging it whenever it changes and I keep getting 0 returned to me).
Here's the relevant section of my code - does anybody have any ideas?
- (void)setupToolBarAndSlider {
CGRect frame = CGRectMake(0.0, 0.0, 200.0, 30.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0;
slider.maximumValue = 8;
slider.continuous = YES;
slider.value = questionNumberForArray;
//Change slider for UIToolbar
UIBarButtonItem *skipQuestionSlider = [[UIBarButtonItem alloc] initWithCustomView:slider];
// Set the width of aSlider
[skipQuestionSlider setWidth:400.0];
//Set up UIToolBar with Slider in it.
UIToolbar *toolBar;
CGRect rect2 = CGRectMake(0, toolBar.frame.origin.y , self.view.frame.size.width , 44);
toolBar = [[UIToolbar alloc]initWithFrame:rect2];
toolBar.barStyle = UIBarStyleBlackTranslucent;
[toolBar setItems:[NSArray arrayWithObjects:skipQuestionSlider, nil]];
[self.view addSubview:toolBar];
}
- (void)sliderValueChanged {
NSInteger roundedSliderNumber = round(skipQuestionSlider.value);
NSLog(#"Slider Value = %d", roundedSliderNumber);
}
Sam
A UIBarButtonItem (in your case, skipQuestionSlider) does not have a value property, it just houses the item. You need to reference the item you put in the UIBarButtonItem.
NSInteger roundedSliderNumber = round(slider.value);