Can someone PLEASE help me out here. I've been scouring the internet for 2 days now looking for ways on how to do this. It seems like it should be simple enough, but it's not.
Just like many others, I'm following along the Stanford iOS 5 tutorials and got to Lecture 8: Controller Lifecycle & Image/Scroll/WebViews. I know in this example, he's using springs and struts, but I'd like to see it get done just as easily in iOS 6.
So, in iOS 5, we just set the contentSize of our UIScrollView = to the size of our UIImageView. However, in iOS 6, we're told NOT to set contentSize.
So, I start off with a new project, drag in the UIImageView, embed it into a UIScrollView. I make sure that Auto Layout is enabled. I create outlets in my view controller for both the scroll view and the image view, then I look to my constraints. This is where I get stuck.
With how great everyone says the new constraints are, I can't believe that something as simple as this can't be so straight forward. I've gone through all my constraints. I've made sure that they're all adequate, yet when I run my app, the scroll view doesn't pan the image.
Now, if I implement the following:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.scrollView removeConstraints:self.scrollView.constraints];
[self.imageView removeConstraints:self.imageView.constraints];
NSDictionary *viewsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.scrollView, #"sv", self.imageView, #"iv", nil];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[sv]|" options:0 metrics:nil views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[sv]|" options:0 metrics:nil views:viewsDictionary]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[iv]|" options:0 metrics:nil views:viewsDictionary]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[iv]|" options:0 metrics:nil views:viewsDictionary]];
}
It works fine, but look at the constraints I set up. It's not like they're complicated. So why can't this be done easily in IB (at least not from everything I've tried)?
If this CAN be done in IB, can someone please show me how?!?!
Related
Since iOS 16 made changes to AVPlayerViewController, the dismiss button ('x') in the top left corner simply doesn't work. The 'tap' is normal, but it feels like the UIButton target doesn't do anything. I feel like I'm missing something obvious. Is there a delegate method or setting that needs to be used for it to work? It all worked perfectly before iOS 16. Implementation below:
AVPlayerViewController *vc = [[AVPlayerViewController alloc] init];
vc.player = [AVPlayer playerWithURL:[NSURL URLWithString:videoInfo.VideoURL]];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:NO completion:nil];
Using animation. It can be fixed but I don't know why.
[self presentViewController:vc animated:YES completion:nil];
I set a LeftBarButton "Cancel" and a RightBarButton "OK" this way
_barButtonOK = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleDone target:self action:#selector(barButtonOKAction)];
[_barButtonOK setTintColor:BUTTON_TEXTCOLOR];
[self.navigationItem setRightBarButtonItems:#[_barButtonOK]];
_barButtonCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:self action:#selector(barButtonCancelAction)];
[_barButtonCancel setTintColor:BUTTON_TEXTCOLOR];
[self.navigationItem setLeftBarButtonItems:#[_barButtonCancel]];
and they look like this
But when I push a ViewController and then pop it back, the OK button looks like being disabled (actually it is still enabled) like this
It is fine with iOS 10 but just grays out with iOS 11 and I don't know why. Any advice will be appreciated.
The problem is gone after the upgrading of iOS/Xcode at some point of time. Definitely it was an iOS defect.
Here are the relevant lines of code:
...
if([top class] == [SitesViewController class]){
BackupsViewController *backup = [[BackupsViewController alloc] initWithStyle:UITableViewStyleGrouped andID:cg_id];
[websites pushViewController:backup animated:NO];
[websites pushViewController:details_controller animated:YES];
websites is a navigation controller, and it is not nil.
Now the first time I run it, it works fine. But if I sign out, and sign back in it doesn't work. In fact, nothing happens. It stays on the same page it is on. I have already checked to make sure nothing is nil. I also know that I am entering into this if statement and none others.
Here is the logout function:
AppDelegate * appDelegate = (GCAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.tabBarController setSelectedIndex:0]
UINavigationController * topViewController = appDelegate.tabBarController.viewControllers[0];
[topViewController presentViewController:loginScreen animated:YES completion:nil];
[topViewController popToRootViewControllerAnimated:TRUE];
[self deteleKeysAndTokens: (NSString*)k_apiSecret withAccessToken: (NSString*)k_accessToken withAccessSecret:(NSString*) k_accessSecret withkAPIKey: (NSString*)k_apiKey];
In case it might help, the backupViewController is downloading a list of websites, and the detailsViewcontroller is getting information about the websites. In fact, after making the call:
[websites pushViewController:backup animated:NO];
[websites pushViewController:details_controller
animated:YES];
(This is from the first bit of code I posted.)
I check to see what the topViewController is:
UIViewController * topView = websites.topViewController;
The topViewController is the details_controller. So I know it is being pushed onto the stack. However, nothing is happening. I am on the same view I started on. I was thinking that maybe it was because I hadn't gotten all the data. But that doesn't explain (a) why it works the first time through and (b) why it just doesn't display a blank page.
It's hard to say what is wrong as from code it's not clear what is the hierarchy of views.
First make sure you are using the correct navigation controller:
UINavigationController * websites = viewController.navigationController;
[websites pushViewController:anotherViewController animated:NO];
Also I'm not sure why your logout function is so complicated. Where do you execute it?
Anyway, its not good to present view controller on navigation controller and then pop it to root. And also both of them are animated...
Please provide more code.
Desired Scenario: create a custom Alert View via iOS6's AutoLayout vs Frames:
1) Create an empty UIView upon the host view (UIController.view):
alertView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[alertView(==300)]" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[alertView]-|" options:0 metrics:nil views:viewsDict]];
This works: I see my alert UIView upon (subview) the host view.
However, attempting to add UILabel to the Alert view bombs.
Seeing that the UIView (1) was drawn, I merely substituted the UILabel in its stead to see if I can get something:
- (UILabel *)createLabelWithMessage:(NSString *)message {
if (!message) return nil;
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0,0)];
myLabel.text = message;
[myLabel setFont:[UIFont systemFontOfSize:12.0]];
[myLabel setAutoresizingMask:UIViewAutoresizingNone];
myLabel.backgroundColor = [UIColor clearColor];
return myLabel;
}
...
titleLabel = [self createLabelWithMessage:#"Danger"];
...
// ...replacing 'alertView' (UIView) with 'titleView' (UILabel):
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[titleLabel(==300)]" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[titleLabel]-|" options:0 metrics:nil views:viewsDict]];
Question: Why would the UILabel bomb but the UIView appears to be drawing okay?
Here's the hint from Xcode:
AutoLayoutContraints[3828:11303] Unable to simultaneously satisfy
constraints.
Probably at least one of the constraints in the
following list is one you don't want. Try this:
(1) look at each
constraint and try to figure out which you don't expect;
(2) find the
code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you
don't understand, refer to the documentation for the UIView property
translatesAutoresizingMaskIntoConstraints) (
"",
"",
"" )
Will attempt to recover by breaking constraint
(Names: '|':UIView:0x128727a0 )>
There must be a hidden property within UILabel that's screwing up my 'Visual-Formatting' language.
..I created another subview of "alertView"; and I get the same error. So apparently I'm only getting a 'good' result when I merely display one (1) UIView (the 'alertView') upon (subview) of the UIController's view; nothing more.
Something hidden is conflicting the simple constraints. And I don't know what.
BTW: I'm using a NIB as the host UIView, with 'use autoLayout' 'ON'.
However, I'll be using this within larger code without 'autolayout' (iOS 4.3+), within
an iOS6-checked routine.
Solution: (per my comment) I had missed setting the 'setTranslatesAutoresizingMaskIntoConstraints' flag to 'NO' for the particular subview; albeit I had done it for its super container.
Example:
[testView setTranslatesAutoresizingMaskIntoConstraints:NO];
Don't forget to use 'setTranslatesAutoresizingMaskIntoConstraints' for ALL member UIViews!
I want to implement pull-down-to-refresh in a UICollectionViewController under iOS 6. This was easy to achieve with a UITableViewController, like so:
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(startRefresh:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
The above implements a nice liquid-drop animation as part of a native widget.
As UICollectionViewController is a "more evolved" UITableViewController one would expect somewhat of a parity of features, but I can't find a reference anywhere to a built-in way to implement this.
Is there a simple way to do this that I'm overlooking?
Can UIRefreshControl be used somehow with UICollectionViewController despite the header and docs both stating that it's meant to be used with a table view?
The answers to both (1) and (2) are yes.
Simply add a UIRefreshControl instance as a subview of .collectionView and it just works.
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(startRefresh:)
forControlEvents:UIControlEventValueChanged];
[self.collectionView addSubview:refreshControl];
That's it! I wish this had been mentioned in the documentation somewhere, even though sometimes a simple experiment does the trick.
EDIT: this solution won't work if the collection is not big enough to have an active scrollbar. If you add this statement,
self.collectionView.alwaysBounceVertical = YES;
then everything works perfectly. This fix taken from another post on the same topic (referenced in a comment in the other posted answer).
I was looking for the same solution, but in Swift. Based on the above answer, I have done the following:
let refreshCtrl = UIRefreshControl()
...
refreshCtrl.addTarget(self, action: "startRefresh", forControlEvents: .ValueChanged)
collectionView?.addSubview(refreshCtrl)
Not forgetting to:
refreshCtrl.endRefreshing()
I was using Storyboard and setting self.collectionView.alwaysBounceVertical = YES; did not work. Selecting the Bounces and Bounces Vertically does the job for me.
The refreshControl property has now been added to UIScrollView as of iOS 10 so you can set the refresh control directly on collection views.
https://developer.apple.com/reference/uikit/uiscrollview/2127691-refreshcontrol
UIRefreshControl *refreshControl = [UIRefreshControl new];
[refreshControl addTarget:self action:#selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged];
self.collectionView.refreshControl = refreshControl;
mjh's answer is correct.
I ran into the issue where if the the collectionView.contentSize was not larger then the collectionView.frame.size, you can not get the collectionView to scroll. You can not set the contentSize property either (at least I couldn't).
If it can't scroll, it won't let you do the pull to refresh.
My solution was to subclass UICollectionViewFlowLayout and overide the method:
- (CGSize)collectionViewContentSize
{
CGFloat height = [super collectionViewContentSize].height;
// Always returns a contentSize larger then frame so it can scroll and UIRefreshControl will work
if (height < self.collectionView.bounds.size.height) {
height = self.collectionView.bounds.size.height + 1;
}
return CGSizeMake([super collectionViewContentSize].width, height);
}