In iOS5, it seems the width of a UISwitch has changed from 94px to 79px. I use the width of that component to calculate how for to the right, to place it in a UITableViewCell.
Is there a way to ask, through the iOS API, what the width of a UISwitch is, WITHOUT adding it to a view yet?
My current thoughts are to keep the two widths I already know in defines, and then check against iOS version, and if >=5 it should be 79px. But that won't work as well if the width of that component changes again sometime.
Yes, because a UISwitch is a control of fixed width and sets and determines its own size, you can simply create it using CGRectZero and then check its dimensions via its frame. This works in iOS4 and iOS5.
On iOS 4 you get a width of 94px and on iOS 5 you will get the width of 79px. You do this like so:
UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
width = mySwitch.frame.size.width;
You can then use the width value to position accordingly in the parent view. Do that by setting the desired x,y position on the UISwitch frame.
Also I suggest you DO set AutoResizingMask margin values on the UISwitch so that it remains in the position you place it regardless of device orientation or type.
Swift solution:
let size = UISwitch.size()
With extension
private var switchSize: CGSize?
extension UISwitch {
class func size() -> CGSize {
if let size = switchSize {
return size
} else {
let view = UISwitch(frame: CGRectZero)
switchSize = view.frame.size
return view.frame.size
}
}
}
Starting in iOS 6.0, you can use intrinsicContentSize to get the natural size for a UISwitch instance.
Related
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.
Hello,
I have UITableView with static content embedded in UIScrollView
so I have just set size in popover:
- (CGSize)contentSizeForViewInPopover
{
return CGSizeMake(600, 670);
}
but, I got different popover heights (see attachments). In IOS4.3 height is bigger than in IOS5.
I don't want to check IOS version and increase/decrease height.
Please advice.
Thanks
As far as I know, when using the navigation controller as popover contents, you have to
set the contentSizeForViewInPopover on the root controller (or anyone that is being shown as the first). (Don't try to set size on the navigation controller).
whenever you push/pop controllers and you want the popover size changed for the new controller on the stack, call setPopoverContentSize:animated: explicitly. A UINavigationControllerDelegate is good for this.
You can dynamically calculate the height of your popover based on the height of your tableview and overriding contentSizeForViewInPopoverView like this:
- (CGSize)contentSizeForViewInPopoverView {
CGFloat width = 200.0; // Currently no way to obtain the width dynamically, only height.
CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
CGFloat height = CGRectGetMaxY(rect);
return (CGSize){width, height};
}
This assumes you have 1 section. If you have more, you just need to use rectForSection to determine the height of each section and add them up.
UIView *stateView = [getSomeUIView thisOne];
CGRect currentFrame = stateView.frame;
if(currentFrame.size.height == 0.0) {
currentFrame.size = CGSizeMake(260, 60);
}
else {
currentFrame.size = CGSizeMake(260, 0);
}
stateView.frame = currentFrame;
I would expect all the subviews would be hidden when the height of the frame is set to zero however this does not happen (in the iPhone 4.0.1 Simulator).
Any suggestions why or alternatives?
I was planing to later animate the frame so it's a sliding effect. I can not use the y position and move it off screen nor can I create a element to hide it behind since I'm working with a background image and everything on top is transparent/alpha layer.
I've got the same problem. Solved it with clipsToBounds property:
stateView.clipsToBounds = YES
Subviews will only change size if you set their springs and struts to do so.
By default, they are set to "stay the SAME width and height, and stay the same distance from top left corner of the parent view".
You can set the springs/struts in Interface Builder, or in code. e.g.:
aSubView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Use UIScrollView instead of UIView. UIScrollView is made to hide "overflow" and works perfectly.
I am trying to determine the size of a UITableCellView. The reason being that I am using one class for different orientations and devices.
The cell contains one subview that is supposed to fill the entire cell. Right know I'm doing this in the UITableViewCell's init method:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
//iPad
subv = [[OrbitView alloc] initWithFrame:CGRectMake(52, 5, 660, 420) ];
}else{
//iPhone
subv = [[OrbitView alloc] initWithFrame:CGRectMake(15, 5, 290, 200) ];
}
Clearly, there must be a better way of doing this, without the magic numbers. How/Where should I set the frame of the subview in the UITableViewCell so that it fills the entire UITableViewCell?
Use the dimensions of the table view that the cell will go in. Set the autoresizingMask to flexible width to handle rotation and accessory views. This code assumes you have set the rowHeight of the table, but you could use a fraction of the screen height instead of testing the device type.
-(CGRect) cellFrameForTableView:(UITableView *)inTable {
CGRect result = [inTable frame];
result.origin = CGPointZero;
result.size.height = [inTable rowHeight];
return result;
}
Add your custom view to the cell content, not the cell, if you want auto sizing to adjust your view to leave room for cell extras like accessory views. In that case things like edit controls will adjust your custom view along with the content view.
In the past I have had trouble with flexible height views in cells, so I would go with flexible bottom margin instead.
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.