I am working on project where i want to make my app compatible with IOS7.
self.contentSizeForViewInPopover = CGSizeMake(90, 1 * 65 - 1);
this method is deprecated from ios7
new method is self.preferredContentSize= CGSizeMake(90,1*65-1);
but i am not getting proper UI
but when i am changing with ios7 compatiable method my view is like this
with ios7 method i am getting this
This is given in apple documentation:
This property allows direction manipulation of the content size of the popover. Changing the property directly is equivalent to animated=YES. The content size is limited to a minimum width of 320 and a maximum width of 600.
#property (nonatomic) CGSize popoverContentSize;
- (void)setPopoverContentSize:(CGSize)size animated:(BOOL)animated;
I have implemented to handle the content size of popover like this & its working:
self.popController.popoverContentSize = CGSizeMake(430, 460);
Related
I'm trying to get a UITextView to dynamically size it's self to it's content. Sometimes the height of the text view will be 100 sometimes 700.
Everything I've tried so far hasn't worked.
I've tried the following in viewDidLoad and also in viewDidLayoutSubviews.
here's the code:
CGRect frameDesc = self.descriptionDeal.frame;
frameDesc.size.height = self.descriptionDeal.contentSize.height;
self.descriptionDeal.frame = frameDesc;
self.descriptionDeal.backgroundColor = [UIColor redColor];
I added the background color so I could see the size of the view and it's not sizing to content.
I also tried:
[self.descriptionDeal sizeToFit];
Also, not sure if it's affecting anything, but in the story where the UITextView is laid out the size is set there. I thought the code above would override it, but somethings not working correctly.
thanks for any help
You should be making the size changes in your text view's delegate's textViewDidChange: method
Use https://stackoverflow.com/a/14956351/1311910 to get the appropriate height.
Try the following string methods which can be used to get the dimensions that would be required to display the string on to a view. You can change the textview's frame based on the CGSize that you get from one of these methods. Use this if you intend not to use auto layout in your project to support earlier versions of iOS.
- (CGSize)sizeWithFont:(UIFont *)font; // Uses NSLineBreakByWordWrapping
- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode;
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size; // Uses NSLineBreakModeWordWrap
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode; // NSTextAlignment is not needed to determine size
I'm having some trouble catering for the new iPhone 5 screen height, I need to resize my table view already to show an advert.
Up till iOS6 I didn't have a problem, I used the following function, but it doesn't use scale. To be honest I'm surprised it works.
+ (CGRect)setTableBoundsByHeight:(int)lHeight:(UITableView*)tbl {
CGRect tableFrame = tbl.frame;
return CGRectMake(tableFrame.origin.x,
tableFrame.origin.y,
tableFrame.size.width,
lHeight);
}
Here's the code, where I have hard coded the height of my table view at 367, that's minus the height of a navigation controller and a tab bar. 50 is the height of the advert.
if (!productPurchased) {
#ifdef VER_FREE
[[LARSAdController sharedManager]
addAdContainerToView:self.view withParentViewController:self];
[[LARSAdController sharedManager]
setGoogleAdPublisherId:#"number"];
[reportTableView setFrame:[General
setTableBoundsByHeight:(367-50):reportTableView]];
#endif
} else {
[reportTableView setFrame:[General
setTableBoundsByHeight:367:reportTableView]];
}
I've found some code which scales but I'm not sure how to implement this.
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
If this code is inside your view controller, just use self.view.bounds.height rather than 367.
By the way: You should really rename
+ (CGRect)setTableBoundsByHeight:(int)lHeight:(UITableView*)tbl
to something like
+ (CGRect)setTableBoundsByHeight:(int)lHeight tableView:(UITableView *)tbl
Ignore the scale, it scales automatically. Just check if iPhone 5 and set different height, but you use iphone5 pixel count/2 as it'll scale it to 2x itself.
Using hard-coded values (a.k.a. "magic numbers") is a wrong habit, you see why now. Always prefer using constants or runtime-computed values. Besides, it makes code easier to read because by using constants you will know what the numbers correspond to, instead of being "magic numbers" coming from nowhere.
So for your problem, compute the height value at runtime using this kind of code below.
// simply use the height of the current viewController's `view`
// which is probably the view of the `navigationController`'s `topViewController`
// and is already at the correct size, namely 367 in iPhone 3.5" and 455 in iPhone 4".
CGFloat screenHeight = self.view.height;
if (!productPurchased)
{
static CGFloat advertHeight = 50;
#ifdef VER_FREE
[[LARSAdController sharedManager]
addAdContainerToView:self.view withParentViewController:self];
[[LARSAdController sharedManager]
setGoogleAdPublisherId:#"number"];
[reportTableView setFrame:[General
setTableBoundsByHeight:(screenHeight-advertHeight):reportTableView]];
#endif
} else {
[reportTableView setFrame:[General
setTableBoundsByHeight:screenHeight:reportTableView]];
}
Note that you don't need to do any substraction yourself, as the UIViewControllers resize their view according to the space available, so that if you have for example a UITabBarController that contains a UINavigationController that itself shows a UIViewController on top of its stack, the height of this last viewController's view will be the height of the screen minus the tabBar, statusBar and navBar heights.
So instead of fetching the [UIScreen mainScreen].applicationFrame for example, and subtract the tabBar (if any) and navBar heights to have your value of 367pt, simply use the height of the viewController's view directly and you should have the right value directly.
Additional note: you should give a prefix to your second argument, thus name your method setTableBoundsByHeight:tableView: instead of setTableBoundsByHeight:: where the second argument does not have any prefix. (See #MrMage answer that suggest this too).
A better naming for your method would even be setHeight:forTableView: for example, to beter fit the Apple naming conventions.
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.
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.
I am trying to construct a iOS universal application template that transparently handles iAds & screen rotations.
i.e. instead of using UIViewController for each new project, I will instead use my own iAdVC (which will subclass UIViewController). This will seamlessly handle the iAds, and hand over the remaining window space to the user.
I'm trying this: view controller contains uberView which contains {adView, content view}.
whenever an ad appears and disappears, both {adView, content view} will animate:
content view squashing the frame's top down slightly to make space for my iAd,
and fade in the ad along the top at the same time.
also, every time the device rotates, the views need to be resized.
I'm getting really dumb problem, when the first Ad gets served, I place it at the top of the screen and squash the remaining content frame to make space for it.
but if I change the content view's frame, I can no longer click the ad. and if I don't, the content view doesn't fit in its window,
http://d.pr/ZyQG
- (void) bannerViewDidLoadAd: (ADBannerView *) banner
{
bool isLandscape = UIInterfaceOrientationIsLandscape( self.interfaceOrientation );
NSString * contentSize = isLandscape ? ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifierPortrait ;
[self.adBannerView setCurrentContentSizeIdentifier: contentSize];
CGSize bannerSize = [ADBannerView sizeFromBannerContentSizeIdentifier: contentSize];
self.adBannerView.frame = CGRectMake(0, 0, bannerSize.width, bannerSize.height);
// resize content frame & fade ad in
CGRect newContentFrame = uberView.bounds;
newContentFrame.size.height -= bannerSize.height;
newContentFrame.origin.y += bannerSize.height;
NSLog(#"%#", NSStringFromCGRect(newContentFrame)); // {{0, 50}, {320, 430}}
if (1) // 0 works
self.contentView.frame = newContentFrame; // NOW CANT CLICK AD
}
Minimum deployment target
The first question is: what is a sensible minimum deployment target? Seeing as this is a universal application, we should use iOS 4.2, as this is the first version that is unified between iPhone and iPad.
Question arises: what fraction of ad-clicking customers do we lose? eg is it worth supporting 4.0 just to get an extra 15% of customers?
http://insights.chitika.com/2011/ios-update-ipads-iphones-running-high-rate-of-ios-4/ shows that you still pick up 80% of ad-clicking customers if you select 4.2.
Obviously this fraction is going to increase with time, so I'm going for the easiest coding option rather than trying to squeeze every last penny out of the market.
This has an added benefit:
// Supported sizes of banner ads available from ad server. Dimensions are in points, not pixels.
// The dimensions are part of the value names to assist with design-time planning for view layout.
extern NSString * const ADBannerContentSizeIdentifier320x50 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_4_0,__IPHONE_4_2);
extern NSString * const ADBannerContentSizeIdentifier480x32 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_4_0,__IPHONE_4_2);
extern NSString * const ADBannerContentSizeIdentifierPortrait __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_2);
extern NSString * const ADBannerContentSizeIdentifierLandscape __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_2);
ie We can use the new symbols, which are generic (ie work for both iPhone and iPad)
strPortrait = ADBannerContentSizeIdentifierPortrait; // ADBannerContentSizeIdentifier320x50;
strLandscape = ADBannerContentSizeIdentifierLandscape; // ADBannerContentSizeIdentifier480x32;
The banner can be either horizontal or vertical, so you need to load in:
[self.adBannerView setRequiredContentSizeIdentifiers:
[NSSet setWithObjects: strPortrait, strLandscape, nil]
];
Then when the screen turns 90°, the AdBannerView needs to be told:
[self.adBannerView setCurrentContentSizeIdentifier: isLandscape ? strLandscape : strPortrait ];
Directly after this is set, you can query self.adBannerView.frame and get the new size
https://github.com/p-i-/iAdUniversalTemplate
This is a XIB-less universal-app iAd-rotatey-enabled template, which requires a minimum target of iOS 4.2
It took a lot of thrashing around, namely iAd -- cannot click banner
But it is in good shape now.