在Objective-C中,我们可以使用以下代码获取设备宽度和高度:
CGRect sizeRect = [UIScreen mainScreen].applicationFrame
float width = sizeRect.size.width
float height = sizeRect.size.height
Run Code Online (Sandbox Code Playgroud)
怎么能用Swift做到这一点?
我问这个(某种程度上)简单的问题只是为了挑剔,因为有时候我担心我可能会对许多UIView的API进行误操作,特别是在涉及自动布局时.
为了使它变得非常简单,我将使用一个例子,假设我需要一个具有图像图标和多行标签的UIView子类; 我想要的行为是我的视图高度随着标签的高度而变化(以适应文本内部),而且,我正在使用Interface builder进行布局,所以我有这样的事情:

使用一些约束来为图像视图提供固定的宽度和高度,并将固定的宽度和位置(相对于图像视图)固定到标签:

现在,如果我在标签上设置了一些文本,我希望视图在高度上调整大小以适应它,或者保持与xib中相同的高度.在自动布局之前我会做到这样的事情:
在CustoView子类文件中,我会sizeThatFits:像这样重写:
- (CGSize) sizeThatFits:(CGSize)size{
//this stands for whichever method I would have used
//to calculate the height needed to display the text based on the font
CGSize labelSize = [self.titleLabel intrinsicContentSize];
//check if we're bigger than what's in ib, otherwise resize
CGFloat newHeight = (labelSize.height <= 21) ? 51: labelSize.height+20;
size.height = newHeight;
return size;
}
Run Code Online (Sandbox Code Playgroud)
而且我会称之为:
myView.titleLabel.text = @"a big text to display that should be more than a line";
[myView …Run Code Online (Sandbox Code Playgroud) 在Objective-C中我能够使用:
CGSize stringsize =
[strLocalTelefone sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];
Run Code Online (Sandbox Code Playgroud)
但在Swift语言中,我没有找到任何解决方案.
任何帮助?
更新之后Xcode8不能设置contentSize为ScrollView.我无法使用CGSizeMake.谁能帮我?
我试过了
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 700)
Run Code Online (Sandbox Code Playgroud) 下面的新编辑
我已经引用了
AVMutableComposition - 仅播放第一曲目(Swift)
但它没有提供我正在寻找的答案.
我有一个AVMutableComposition().我试图在这个单一的组合中应用MULTIPLE AVCompositionTrack,单一类型AVMediaTypeVideo.这是因为我用2个不同的AVMediaTypeVideo具有不同来源CGSize的和preferredTransforms的的AVAsset"他们来自秒.
因此,应用指定的唯一方法preferredTransforms是在两个不同的轨道中提供它们.但是,无论出于何种原因,只有第一首曲目实际上会提供任何视频,几乎就像第二首曲目从未出现过一样.
所以,我试过了
1)使用AVMutableVideoCompositionLayerInstruction's并且应用一个AVVideoComposition,一个AVAssetExportSession正常工作,我仍然在进行变换,但是可行.但视频的处理时间超过1分钟,这在我的情况下是不适用的.
2)使用多个音轨,AVAssetExportSession不会出现相同类型的第二个音轨.现在,我可以将它全部放在一个轨道上,但所有视频将与第一个视频相同的尺寸和首选视频,这是我绝对不想要的,因为它会在各个方向上拉伸它们.
所以我的问题是,是否有可能
1)在没有使用的情况下将指令应用于轨道AVAssetExportSession?//首选方式BY FAR.
2)减少出口时间?(我已经尝试过使用PresetPassthrough但你不能使用它,如果你有exporter.videoComposition我的指示所在的地方.这是我知道我可以放指令的唯一地方,不知道我是否可以将它们放在其他地方.
这是我的一些代码(没有导出器,因为我不需要在任何地方导出任何东西,只需在AVMutableComposition组合项目后执行操作.
func merge() {
if let firstAsset = controller.firstAsset, secondAsset = self.asset {
let mixComposition = AVMutableComposition()
let firstTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo,
preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
//Don't need now according to not being able to edit …Run Code Online (Sandbox Code Playgroud) CGSize和CGRect有什么区别?
作为初读者阅读CGGeometry参考文献,它并不是立即显而易见的,特别是因为大小是一个向量的引用.我很惊讶我在StackOverflow上找不到这个基本问题,所以我决定研究它并在下面发表我的答案.
我一直试图使用cgsizemake如下:
我正在尝试使用cgrectmake不改变坐标来使我的图像的框架具有不同的尺寸.
到目前为止我已经尝试过
maincharacter.frame = cgsizemake (14, 14);
Run Code Online (Sandbox Code Playgroud)
但我一直在收到错误
从不兼容的类型CGsize分配到cgrect
提前致谢.
我正在用swift编写一个程序,刚才我注意到我可以直接访问CGRect框架的宽度和高度属性而不使用CGSize宽度和高度.那就是我现在能够编写这样的代码.
@IBOutlet var myView: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
var height = myView.frame.height
var height1 = myView.frame.size.height
}
Run Code Online (Sandbox Code Playgroud)
在Objective C中,当我尝试编写相同的代码时,该行height = view.frame.height会抛出错误.任何人都可以告诉我这两行代码中的差异(如果有的话).
我在Xcode 8中将我的项目更新为Swift3,它出现了这个错误,但我不知道我能在那里制作什么.我已经在谷歌搜索但没有创建.有谁有想法我能做什么?
这里错误:
使用Objective-C选择器'collectionViewContentSize'的方法'collectionViewContentSize()'与来自超类'UICollectionViewLayout'的'collectionViewContentSize'的getter冲突,具有相同的Objective-C选择器
public func collectionViewContentSize() -> CGSize {
let numberOfSections = collectionView?.numberOfSections
if numberOfSections == 0 {
return CGSize.zero
}
var contentSize = collectionView?.bounds.size
contentSize?.height = CGFloat(columnHeights[0])
return contentSize!
}
Run Code Online (Sandbox Code Playgroud) cgsize ×10
ios ×7
swift ×5
cgrect ×2
swift3 ×2
autolayout ×1
avasset ×1
cocoa-touch ×1
contentsize ×1
objective-c ×1
uiimage ×1
uiscreen ×1
uiscrollview ×1
uiview ×1
xcode6 ×1
xcode8 ×1