Progress view height in iOS 7 - iphone

I want to increase the height of progress view in iOS 6 and below i am doing this using appearence method
UIImage *progressImage = [[UIImage imageNamed:#"sliderbk-progress.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 5)];
[[UIProgressView appearance] setProgressImage:progressImage];
but now in iOS7 this code is not working i even try given below code but no use. Any help will be helpfull. Thanks
[[UIProgressView appearance] setFrame:CGRectMake(20, 100, 280, 100)];

If I am understanding the question correctly it sounds like you want to increase the height of the progress view in iOS7, and the code you used previously in iOS6 is no longer working.
I had to solve a similar problem recently and I did this by adding a constraint to the progress view in Interface Builder and setting the height explicitly through the constraint. This solution will require the use of Auto-Layout, so be sure that you have that turned on.
Shown: the "Height" attribute on the Size Inspector is visibly greyed out for a Progress View and cannot be changed - however I've defined a constraint on the Progress View itself and set the constraint's height to 50 points, which is actually reflected in IB.
From what I've seen iOS6 Progress Bars have a static height value, so if you also want to support iOS6 then another approach will be necessary for that.

Whereas others have reported that a CGAffineTransform() works as well:
[self.progressView setTransform:CGAffineTransformMakeScale(1.0, 3.0)];

Already answered here
How to increase height of UIProgressView
#implementation UIProgressView (customView)
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize newSize = CGSizeMake(self.frame.size.width,9);
return newSize;
}
#end

Here's the Swift version of user3189408 and Rushabh's great solutions for newer developers and swift enthusiasts like me. Tested for iOS 7+/Swift 2.0.
progressView.transform = CGAffineTransformMakeScale(1.0, 5.0)

Swift 3.x
progressView.transform = CGAffineTransform(scaleX: 1.0, y: 5.0)

You can note that frame cannot be set by appearance accessor. You have to set it on each progress view separately.
Usually, the height is set depending on progress bar style.
- (id)initWithProgressViewStyle:(UIProgressViewStyle)style; // sets the view height according to the style

If you're using Autolayout, then the solution is simple: create a height constraint as explained by one of the answers here.
However, chances are you're here because you're creating the progress bar in code.
In this case, solving this through the transform method is not ideal if the view has round corners, since CGAffineTransform will mess with how the corner radius is drawn.
I would subclass the UIProgressView as follows:
class ProgressBarThick: UIProgressView {
var height : CGFloat = 12
var width: CGFloat = 0.0
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: width,
height: height)
}
}
Set the height and width before drawing the view.

Related

Swift - programmatically set UIScrollView width to 100%

In the designer I added a Scrollview, I made it have a scrollbar with this code:
svDetails.contentSize = CGSizeMake(360, 400);
However I want it to be 100% of the phone width. (leave the width unchanged).
Because right now it fits perfectly on iPhone 4s,5 and 5s but doesn't look quite right on the iPhone 6,6+ (scrollview is to small)
You can use this code:
svDetails.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)
Hope it helps.
Dharmesh Kheni' solution from above in
Swift 3:
svDetails.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height)

UITextField - (void)drawPlaceholderInRect:(CGRect)rect returns different CGRect height in iOS 7

I tried to subclass UITextField to draw custom placehoder. In iOS 6 this works fine but in iOS 7 I got a different CGRect height.
The UITextField frame is (0, 0, 500, 45). I added a left padding of 20 by overriding
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
Calling the method below to do so:
- (CGRect)makeRectFromBounds:(CGRect)bounds
withTopPadding:(CGFloat)topPadding
andLeftPadding:(CGFloat)leftPadding
{
return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(topPadding, leftPadding, 0, 0));
}
Because I want a different placeHolder text color, I override
- (void)drawPlaceholderInRect:(CGRect)rect
- (void)drawPlaceholderInRect:(CGRect)rect {
[[UIColor colorWithRed:121.0/255.0
green:113.0/255.0
blue:107.0/255.0
alpha:1.0] setFill];
[self printRect:rect from:__FUNCTION__];
[[self placeholder] drawInRect:rect withFont:self.font];
}
The rectangle I'm printing is the following:
iOS 7: -Rect (X: 0.0, Y:0.0, W:480.0, H:44.0)
iOS 6: -Rect (X: 0.0, Y:0.0, W:480.0, H:26.0)
Any idea if this is a bug or am I doing something wrong?
Use the following instead:
[textfield setValue:[UIColor redColor] forKeyPath:#"_placeholderLabel.textColor"];
In iOS 7 the default value for contentVerticalAlignment changed from "top" to "center" (with no documentation that I could see). In "center" mode iOS adjusts the result of the rectForBounds methods before drawing into them. You should probably set contentVerticalAlignment = UIControlContentVerticalAlignmentTop when overriding any of textRectForBounds methods so iOS will use the rect exactly as specified.
Since iOS7 is new nowadays, so many people faces framing issue with iOS7.
To all of them, I just want say that it is so easy and there isn't any issue with iOS7. It's just because you don't know how to take benefits from the latest OS feature provided by Apple.
#Cyupa : You just need to apply autosizing and mask your textfield.
it could be one or more from following.
UIViewAutoresizingFlexibleBottomMargin
UIViewAutoresizingFlexibleTopMargin
UIViewAutoresizingFlexibleLeftMargin
UIViewAutoresizingFlexibleRightMargin
UIViewAutoresizingFlexibleHeight
UIViewAutoresizingFlexibleWidth
if you apply proper autosizingmask to your textfield, you will get your desired frame for your view (Here textfield)
I also met this problem, not found why yet, but if you want to have the same behavior on both iOS6 and iOS7, you can try this:
- (CGRect)textRectForBounds:(CGRect)bounds {
CGRect rect = [super textRectForBounds:bounds];
rect = CGRectMake(20, rect.origin.y-4, rect.size.width-20, rect.size.height);
return rect;
}
and you may need to set:
theLabel.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
check the system version and return the UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(topPadding, leftPadding, 0, 0));
you can check device version as
if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0))
{
}
i solved issue by using this property
textField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
It's working for both iOS6 and iOS7.
Ricky's solution worked for me with the addition this value has to be set each time AFTER placeholder text changed. I overwrote setPlaceholder to do that.
It replaces the need to overwrite drawPlaceholderInRect, if you want another placeholder color, therefore vertical alignment will be correct automatically. Of course this does not answer the question, why IOS 7.0 behaves like that, but it might solve your actual problem.
- (void) setPlaceholder: (NSString*)placeholderText {
[super setPlaceholder:placeholderText];
[self setValue:[UIColor redColor] forKeyPath:#"_placeholderLabel.textColor"];
}
It should be mentioned, that some people discourage this bypassing of public interfaces, so do it on your own risk!
See also:
Change UITextField's placeholder text color programmatically

How to set UITextField height?

I am using a UITextField. I want to increase its height but I have not found any property to do this. How can I achieve this?
You can not change the height of the rounded rect border style.
To set the height, just choose any border style other than rounded border in Xcode:
I finally found the fix for this!
As we have found, IB doesn't allow us to change the height of the rounded corner border style. So change it to any of the other styles and set the desired height. In the code change the border style back.
textField.borderStyle = UITextBorderStyleRoundedRect;
CGRect frameRect = textField.frame;
frameRect.size.height = 100; // <-- Specify the height you want here.
textField.frame = frameRect;
If you are using Auto Layout then you can do it on the Story board.
Add a height constraint to the text field, then change the height constraint constant to any desired value. Steps are shown below:
Step 1: Create a height constraint for the text field
Step 2: Select Height Constraint
Step 3: Change Height Constraint's constant value
1.) Change the border Style in the InterfaceBuilder.
2.) After that you're able to change the size.
3.) Create an IBOutlet to your TextField and enter the following code to your viewDidLoad() to change the BorderStyle back.
textField.borderStyle = UITextBorderStyleRoundedRect;
Swift 3:
textField.borderStyle = UITextBorderStyle.roundedRect
Choose the border style as not rounded
Set your height
in your viewWillAppear set the corners as round
yourUITextField.borderStyle = UITextBorderStyleRoundedRect;
Enjoy your round and tall UITextField
Follow these two simple steps and get increase height of your UItextField.
Step 1: right click on XIB file and open it as in "Source Code".
Step 2: Find the same UITextfield source and set the frame as you want.
You can use these steps to change frame of any apple controls.
An update for iOS 6 : using auto-layout, even though you still can't set the UITextField's height from the Size Inspector in the Interface Builder (as of Xcode 4.5 DP4 at least), it is now possible to set a Height constraint on it, which you can edit from the Interface Builder.
Also, if you're setting the frame's height by code, auto-layout may reset it depending on the other constraints your view may have.
I know this an old question but I just wanted to add if you would like to easily change the height of a UITextField from inside IB then simply change that UITextfield's border type to anything other than the default rounded corner type. Then you can stretch or change height attributes easily from inside the editor.
swift3
#IBDesignable
class BigTextField: UITextField {
override func didMoveToWindow() {
super.didMoveToWindow()
if window != nil {
borderStyle = .roundedRect
}
}
}
Interface Builder
Replace UITextField with BigTextField.
Change the Border Style
to none.
My pathetic contribution to this dumb problem. In IB set the style to none so you can set the height, then in IB set the class to be a subclass of UITextField that forces the style to be rounded rect.
#interface JLTForcedRoundedRectTextField : UITextField
#end
#implementation JLTForcedRoundedRectTextField
- (void)awakeFromNib
{
self.borderStyle = UITextBorderStyleRoundedRect;
}
#end
It kept me from having to hack the XIB file or writing style code into my view controller.
A UITextField's height is not adjustable in Attributes Inspector only
when it has the default rounded corners border style, but adding a
height constraint (plus any other constraints which are required to
satisfy the autolayout system - often by simply using Add Missing
Constraints) to it and adjusting the constraint will adjust the
textfield's height. If you don't want constraints, the constraints can
be removed (Clear Constraints) and the textfield will remain at the
adjusted height.
Works like a charm.
In Swift 3 use:
yourTextField.frame.size.height = 30
try this
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 280, 120)];
UITextField *txt = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[txt setText:#"Ananth"];
[self.view addSubview:txt];
Last two arguments are width and height, You can set as you wish...
You can use frame property of textfield to change frame
Like-Textfield.frame=CGRECTMake(x axis,y axis,width,height)
This is quite simple.
yourtextfield.frame = CGRectMake (yourXAxis, yourYAxis, yourWidth, yourHeight);
Declare your textfield as a gloabal property & change its frame where ever you want to do it in your code.
Happy Coding!
If you're creating a lot of UITextFields it can be quicker to subclass UITextViews and override the setFrame method with
-(void)setFrame:(CGRect)frame{
[self setBorderStyle:UITextBorderStyleRoundedRect];
[super setFrame:frame];
[self setBorderStyle:UITextBorderStyleNone];
}
This way you can just call
[customTextField setFrame:<rect>];
I was having the same issue. tried some of the solutions here but rather than doing all this mumbo-jumbo. I found just setting height constraint is enough.

Custom UIProgressView

I'm using a UIProgressView in my application, which is working great right now. But I want to make it larger (height-wise) and color it a different color.
I'm looking to use PDColoredProgressView for the color, and alter the -(void)drawRect:(CGRect)rect method to change the height, however I can't decide where I would actually alter the height. Any suggestions on what to change?
So it turns out you can resize it like any other view.
[coloredProgressView setFrame:CGRectMake(0, 0, 300, 25)];
Setting the frame side didn't seem to work for me. Setting the transform to a CGAffineTransformMakeScale() can scale it up - not sure if that causes any other problems though.
To change the height of progressView try below code: ( works with Swift 5 )

progressView.transform = CGAffineTransform(scaleX: 1, y: 4) // y present the wanted height
and 1 present the current width, so if you change it to 3 then it will mean current width x 2

Is there a way to change the height of a UIToolbar?

I've got an UIToolbar in Interface Builder and I've noticed that it's locked to being 44px tall. Of course I'd like to make this larger.
Does Apple allow resizing of this control? If so, how do I go about it?
Sure, just set its frame differently:
[myToolbar setFrame:CGRectMake(0, 50, 320, 35)];
This will make your toolbar 35 pixels tall. Of course this requires an IBOutlet or creating the UIToolbar programmatically, but that's very easy to do.
If that does not work in SDK 6, it is possible to solve as below:
Select the toolbar element and choose Editor > Pin > Height to create a constraint.
Go to your View Controller Scene and select the created Height(44) constraint, then put the value you want.
I found that if I set the frame on the iPad, when hiding/showing the Toolbar would reset itself back to a height of 44 pixels. I ended up having to override UIToolbar and change the method:
// return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (CGSize)sizeThatFits:(CGSize)size {
CGSize result = [super sizeThatFits:size];
result.height = 55;
return result;
};
This would correct adjust the height even with the hide/show.
In iOS 6, with autolayout, the simplest approach is a UIToolbar subclass in which you override instrinsicContentSize. Here's code from one my apps, where the toolbar is tall. Its sides and bottom are pinned to the sides and bottom of the superview as usual.
-(CGSize)intrinsicContentSize {
return CGSizeMake(UIViewNoIntrinsicMetric, 85);
}
For Xcode 7.1 iOS 9, in auto layout, the size is locked to 44px. The Xcode menu option Editor > Pin > Height is not there, instead do the following action:
In InterfaceBuilder, click the toolbar element to select it.
Control+Drag down anywhere in the toolbar and release, a popup menu will display showing the option "Height" at the top, select it.
You now have a Height constraint to work with and adjust as necessary.
You could also just edit the xib file:
open it as source code and find the entry that defines the frame for the UIToolbar, something along the lines of
<string key="NSFrame">{{0,420}, {320,44}}</string>
and just change the value for 44 to whatever size you need.
This way the toolbar will be taller, and in InterfaceBuilder you'll see the new size grayed out and you'll be unable to change it, but you don't need any outlets or code.
As long as you have a height constraint on the toolbar you can use this little snippet that has helped me adjust heights for classes that inherit from UIView
-(void)setHeightConstraintTo:(CGFloat)height forView:(UIView *)view{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"firstAttribute = %d", NSLayoutAttributeHeight];
NSArray *filteredArray = [view.constraints filteredArrayUsingPredicate:predicate];
if(filteredArray.count > 0){
NSLayoutConstraint *constraint = filteredArray.firstObject;
constraint.constant = height;
}
}
I'm not sure how this would sit with Apple - and of course it depends how you wish to use the toolbar - but you can add a default UIView and change its class in the property inspector to UIToolbar. This gives you transparency and customisability (in this case height) for free, at the expense of the layout of bar button items.
Swift Solution:
myToolbar.frame = CGRect(x: myToolbar.frame.origin.x, y: myToolbar.frame.origin.y, width: myToolbar.frame.size.width, height: 20)
The CGRectMake is obsolete. This can be replaced with the CGRect. This will set the height of the toolbar to 20. The same works for Segmented control as well.
In interface builder, there is also the possibility to use "User Defined Runtime Attributes".
Simply add an entry with keypath set to "frame" of type "Rect" and set the value you want.