如何使用自动布局以编程方式将UIView设置为其超级视图的中心?
UIButton* viewObj = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewObj setTranslatesAutoresizingMaskIntoConstraints:NO];
[viewObj setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:viewObj];
NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:viewObj
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
[self.view addConstraint:cn];
cn = [NSLayoutConstraint constraintWithItem:viewObj
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0];
[self.view addConstraint:cn];
Run Code Online (Sandbox Code Playgroud)
上面的代码适用于UIButton,但是我在使用适用于UIView的东西替换第一行时遇到了麻烦.
我试过了
UIView* viewObj = [UIView alloc] initWithFrame:CGRectZero];
Run Code Online (Sandbox Code Playgroud)
但视图不会出现在模拟器中.
有什么建议?
谢谢!
我正在实现一个自定义的UICollectionViewCell,我想知道如何向它发送模型数据,以便数据可用于初始化单元的子视图.
我通过做注册我的MyCollectionViewCell
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
Run Code Online (Sandbox Code Playgroud)
在以下方法中,我做...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,当一个单元不在重用队列中时,它通过调用initWithFrame方法(文档确认应该发生)来初始化一个新单元.但是,我的MyCollectionViewCell类中有一个自定义的init方法,它是......
-(id) initWithFrame:(CGRect)frame withData: (NSDictionary*) data;
Run Code Online (Sandbox Code Playgroud)
我希望调用自定义初始化方法.我该怎么办?我没有使用任何笔尖,我正在以编程方式执行所有操作.
如果没有办法,我必须使用initWithFrame方法,我还能如何将模型数据传递给MyCollectionViewCell,以便我可以用该数据初始化子视图?
谢谢!
我正在尝试将PKRevealController集成到我的现有项目中.
https://github.com/pkluz/PKRevealController
如果我正在使用故事板,如何设置左视图控制器,右视图控制器和前视图控制器?自述文件说...
PKRevealController *revealController = [PKRevealController revealControllerWithFrontViewController:frontVC leftViewController:leftVC options:options];
self.window.rootViewController = revealController;
Run Code Online (Sandbox Code Playgroud)
我会在现有的故事板项目中将这些代码行放在哪里?或者有没有其他选择来设置它?
谢谢!
如果您运行以下代码会发生什么?
while (true) {
String x = new String("ABC");
}
Run Code Online (Sandbox Code Playgroud)
在记忆方面?
字符串x是在堆栈上还是在堆上分配的?由于内存溢出,程序最终会崩溃,还是垃圾收集会阻止它?new关键字是否始终在堆上创建对象?何时在堆栈上创建对象?
谢谢!
我想只在我的iOS应用程序的所有视图控制器中支持垂直方向.但是,我在我的一个视图控制器中嵌入了一个YouTube视频,当选择该视频占据整个屏幕时,我希望用户能够水平定位他/她的手机,以便视频扩展为全屏.
编辑:我尝试使用iOS 6中的Autorotate以下代码有奇怪的行为:
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return self.fullScreenVideoIsPlaying ?
UIInterfaceOrientationMaskAllButUpsideDown :
UIInterfaceOrientationMaskPortrait;
Run Code Online (Sandbox Code Playgroud)
}
在我的视图控制器中显示UIWebView/ YouTube框架,我在我的代码中有这样的代码viewDidLoad:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(windowNowVisible:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window
];
- (void)windowNowVisible:(NSNotification *)notification
{
AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
appDelegate.fullScreenVideoIsPlaying = !(appDelegate.fullScreenVideoIsPlaying);
}
Run Code Online (Sandbox Code Playgroud)
然而,当用户在全屏YouTube视频上按下完成时,如果他/她仍然水平地具有电话,则呈现视图控制器也保持水平(我希望当前视图控制器是肖像).这是fullSreenVideoIsPlaying变量的竞赛,它没有足够快地更新,因此我的呈现视图控制器是纵向的.
任何有关如何实现这一目标的反馈将不胜感激.
谢谢!
当你只有几个单元格时,如何使UICollectionView可滚动?
我已经实现了一个UICollectionView,它以网格的形式布置单元格.如果只有几个单元格,则默认情况下,UICollectionView不可滚动(所有单元格都适合手机屏幕).
但是,我还实现了PullToRefreshView.当我有很多单元格时,这非常有效,因为UICollectionView可以滚动,我可以拉动和刷新.
尽管只有几个单元格,但我能做些什么来使UICollectionView可滚动?
这是我初始化PullToRefreshView的方法
self.pull = [[PullToRefreshView alloc] initWithScrollView:self.collectionView];
[self.collectionView addSubview:self.pull];
Run Code Online (Sandbox Code Playgroud) 在我的iOS应用程序中,我正在从网上下载jpeg图像,我想知道如何找到图像的正确宽度和高度,以便我可以在我的iOS应用程序中正确显示它.
例如,

如何获得此jpg图像的宽度和高度?
如何设置AppDelegate和ViewController成为模型重定位类的监听器?什么是正确的设计选择?
我有兴趣有一个模型类来实现CoreLocation和位置更新.我猜这个课应该是一个sharedSingleton,因为我AppDelegate和他都ViewController希望访问它.
当我viewController打电话给它时,我想要CLLocationManager使用它startUpdatingLocation.
当应用程序进入后台时,我想使用startMonitoringSignificantLocationChanges监视AppDelegate中的位置更新.
我的问题是,如何设置模型类来处理这些不同类型的位置更新,以及通知ViewController或AppDelegate找到新的位置?用NSNotification?授权似乎不起作用,因为它是一对一的关系.
感谢您在确定如何设计时的帮助.
谢谢!
如何检查CLLocation对象并决定是否要使用它或丢弃结果并获取新的位置更新?
我在CLLocation上看到了timestamp属性,但我不确定如何将它与当前时间进行比较.
此外,在我比较时间并找到以秒为单位的差异之后,我应该使用该CLLocation对象的差异是什么值?什么是一个很好的门槛.
谢谢!
我想知道如何设置我的UICollectionView每个部分最多可以选择1个单元格.我看到有allowsMultipleSelectionUICollectionView 的属性,但我想知道如何防止在同一部分中选择多个单元格.
我是否需要在– collectionView:shouldSelectItemAtIndexPath:和collectionView:shouldDeselectItemAtIndexPath:方法中实现逻辑或者是否有更简单的方法?
谢谢!
ios ×9
iphone ×7
objective-c ×4
autolayout ×1
heap ×1
image ×1
ios5 ×1
ios6 ×1
java ×1
jpeg ×1
memory ×1
nsdate ×1
scroll ×1
stack ×1
storyboard ×1